Skip to content

Commit 004bf2e

Browse files
authored
Merge pull request #76 from montaro/logging-interface-handle-buffer-overflow
Enable defining a handler for buffer overflow when using the library as a python logging handler
2 parents 093cc13 + dcfee05 commit 004bf2e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/.tox
1010
/build
1111
/dist
12+
.idea/

README.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module.
188188
189189
logging.basicConfig(level=logging.INFO)
190190
l = logging.getLogger('fluent.test')
191-
h = handler.FluentHandler('app.follow', host='host', port=24224)
191+
h = handler.FluentHandler('app.follow', host='host', port=24224, buffer_overflow_handler=handler)
192192
formatter = handler.FluentRecordFormatter(custom_format)
193193
h.setFormatter(formatter)
194194
l.addHandler(h)
@@ -211,6 +211,18 @@ You can also customize formatter via logging.config.dictConfig
211211
212212
logging.config.dictConfig(conf['logging'])
213213
214+
You can inject your own custom proc to handle buffer overflow in the event of connection failure. This will mitigate the loss of data instead of simply throwing data away.
215+
216+
.. code:: python
217+
218+
import msgpack
219+
from io import BytesIO
220+
221+
def handler(pendings):
222+
unpacker = msgpack.Unpacker(BytesIO(pendings))
223+
for unpacked in unpacker:
224+
print(unpacked)
225+
214226
A sample configuration ``logging.yaml`` would be:
215227

216228
.. code:: python
@@ -242,6 +254,7 @@ A sample configuration ``logging.yaml`` would be:
242254
host: localhost
243255
port: 24224
244256
tag: test.logging
257+
buffer_overflow_handler: handler
245258
formatter: fluent_fmt
246259
level: DEBUG
247260
none:

fluent/handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,14 @@ def __init__(self,
115115
host='localhost',
116116
port=24224,
117117
timeout=3.0,
118-
verbose=False):
118+
verbose=False,
119+
buffer_overflow_handler=None):
119120

120121
self.tag = tag
121122
self.sender = sender.FluentSender(tag,
122123
host=host, port=port,
123-
timeout=timeout, verbose=verbose)
124+
timeout=timeout, verbose=verbose,
125+
buffer_overflow_handler=buffer_overflow_handler)
124126
logging.Handler.__init__(self)
125127

126128
def emit(self, record):

0 commit comments

Comments
 (0)