8000 Add FluentSender section to README · dotlambda/fluent-logger-python@28997a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28997a8

Browse files
committed
Add FluentSender section to README
1 parent 3a49955 commit 28997a8

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

README.rst

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,60 @@ To quickly test your setup, add a matcher that logs to the stdout:
6060
Usage
6161
-----
6262

63+
FluentSender Interface
64+
~~~~~~~~~~~~~~~~~~~~~~
65+
66+
`sender.FluentSender` is a structured event logger for Fluentd.
67+
68+
By default, the logger assumes fluentd daemon is launched locally. You
69+
can also specify remote logger by passing the options.
70+
71+
.. code:: python
72+
73+
from fluent import sender
74+
75+
# for local fluent
76+
logger = sender.FluentSender('app')
77+
78+
# for remote fluent
79+
logger = sender.FluentSender('app', host='host', port=24224)
80+
81+
For sending event, call `emit` method with your event. Following example will send the event to
82+
fluentd, with tag 'app.follow' and the attributes 'from' and 'to'.
83+
84+
.. code:: python
85+
86+
# Use current time
87+
logger.emit('follow', {'from': 'userA', 'to': 'userB'})
88+
89+
# Specify optional time
90+
cur_time = int(time.time())
91+
logger.emit_with_time('follow', cur_time, {'from': 'userA', 'to':'userB'})
92+
93+
You can detect an error via return value of `emit`. If an error happens in `emit`, `emit` returns `False` and get an error object using `last_error` method.
94+
95+
.. code:: python
96+
97+
if not logger.emit('follow', {'from': 'userA', 'to': 'userB'}):
98+
print(logger.last_error)
99+
logger.clear_last_error() # clear stored error after handled errors
100+
101+
If you want to shutdown the client, call `close()` method.
102+
103+
.. code:: python
104+
105+
logger.close()
106+
63107
Event-Based Interface
64108
~~~~~~~~~~~~~~~~~~~~~
65109

66-
First, you need to call ``logger.setup()`` to create global logger
110+
This API is a wrapper for `sender.FluentSender`.
111+
112+
First, you need to call ``sender.setup()`` to create global `sender.FluentSender` logger
67113
instance. This call needs to be called only once, at the beggining of
68114
the application for example.
69115

70-
By default, the logger assumes fluentd daemon is launched locally. You
71-
can also specify remote logger by passing the options.
116+
Initialization code of Event-Based API is below:
72117

73118
.. code:: python
74119
@@ -81,7 +126,7 @@ can also specify remote logger by passing the options.
81126
sender.setup('app', host='host', port=24224)
82127
83128
Then, please create the events like this. This will send the event to
84-
fluent, with tag 'app.follow' and the attributes 'from' and 'to'.
129+
fluentd, with tag 'app.follow' and the attributes 'from' and 'to'.
85130

86131
.. code:: python
87132
@@ -93,11 +138,14 @@ fluent, with tag 'app.follow' and the attributes 'from' and 'to'.
93138
'to': 'userB'
94139
})
95140
96-
If you want to shutdown the client, call `close()` method.
141+
`event.Event` has one limitation which can't return success/failure result.
142+
143+
Other methods for Event-Based Interface.
97144

98145
.. code:: python
99146
100-
sender.close()
147+
sender.get_global_sender # get instance of global sender
148+
sender.close # Call FluentSender#close
101149
102150
Handler for buffer overflow
103151
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -114,7 +162,7 @@ You can inject your own custom proc to handle buffer overflow in the event of co
114162
for unpacked in unpacker:
115163
print(unpacked)
116164
117-
sender.setup('app', host='host', port=24224, buffer_overflow_handler=handler)
165+
logger = sender.FluentSender('app', host='host', port=24224, buffer_overflow_handler=handler)
118166
119167
You should handle any exception in handler. fluent-logger ignores exceptions from ``buffer_overflow_handler``.
120168

0 commit comments

Comments
 (0)
0