@@ -282,6 +282,72 @@ A sample configuration ``logging.yaml`` would be:
282
282
level: DEBUG
283
283
propagate: False
284
284
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
+
285
351
Testing
286
352
-------
287
353
0 commit comments