8000 Update README.rst with information on the asynchronous threaded commu… · fluent/fluent-logger-python@3a3e97b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a3e97b

Browse files
committed
Update README.rst with information on the asynchronous threaded communication interface.
1 parent d920d71 commit 3a3e97b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,72 @@ A sample configuration ``logging.yaml`` would be:
282282
level: DEBUG
283283
propagate: False
284284
285+
Asynchronous Communication
286+
~~~~~~~~~~~~~~~~~~~~~~~~~~
287+
288+
Besides the regular interfaces - the event-based one provided by ``sender.FluentSender`` and the python logging one
289+
provided by ``handler.FluentHandler`` - there are also corresponding asynchronous versions in ``asyncsender`` and
290+
``asynchandler`` respectively. These versions use a separate thread to handle the communication with the remote fluentd
291+
server. In this way the client of the library won't be blocked during the logging of the events, and won't risk going
292+
into timeout if the fluentd server becomes unreachable. Also it won't be slowed down by the network overhead.
293+
294+
The interfaces in ``asyncsender`` and ``asynchandler`` are exactly the same as those in ``sender`` and ``handler``, so it's
295+
just a matter of importing from a different module.
296+
297+
For instance, for the event-based interface:
298+
299+
.. code:: python
300+
301+
from fluent import asyncsender as sender
302+
303+
# for local fluent
304+
sender.setup('app')
305+
306+
# for remote fluent
307+
sender.setup('app', host='host', port=24224)
308+
309+
# do your work
310+
...
311+
312+
# IMPORTANT: before program termination, close the sender
313+
sender.close()
314+
315+
or for the python logging interface:
316+
317+
.. code:: python
318+
319+
import logging
320+
from fluent import asynchandler as handler
321+
322+
custom_format = {
323+
'host': '%(hostname)s',
324+
'where': '%(module)s.%(funcName)s',
325+
'type': '%(levelname)s',
326+
'stack_trace': '%(exc_text)s'
327+
}
328+
329+
logging.basicConfig(level=logging.INFO)
330+
l = logging.getLogger('fluent.test')
331+
h = handler.FluentHandler('app.follow', host='host', port=24224, buffer_overflow_handler=overflow_handler)
332+
formatter = handler.FluentRecordFormatter(custom_format)
333+
h.setFormatter(formatter)
334+
l.addHandler(h)
335+
l.info({
336+
'from': 'userA',
337+
'to': 'userB'
338+
})
339+
l.info('{"from": "userC", "to": "userD"}')
340+
l.info("This log entry will be logged with the additional key: 'message'.")
341+
342+
...
343+
344+
# IMPORTANT: before program termination, close the handler
345+
h.close()
346+
347+
**NOTE**: please note that it's important to close the sender or the handler at program termination. This will make
348+
sure the communication thread terminates and it's joined correctly. Otherwise the program won't exit, waiting for
349+
the thread, unless forcibly killed.
350+
285351
Testing
286352
-------
287353

0 commit comments

Comments
 (0)
0