8000 Incorporate formatter into handler. · moriyoshi/fluent-logger-python@17152bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 17152bd

Browse files
committed
Incorporate formatter into handler.
1 parent 61c57b2 commit 17152bd

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

fluent/handler.py

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,33 @@
1313

1414
from fluent import sender
1515

16-
class FluentRecordFormatter(object):
17-
def __init__(self, encoding='utf-8'):
16+
class FluentHandler(logging.Handler):
17+
'''
18+
Logging Handler for fluent.
19+
'''
20+
def __init__(self,
21+
tag,
22+
host='localhost',
23+
port=24224,
24+
timeout=3.0,
25+
verbose=False,
26+
encoding='utf-8'):
27+
logging.Handler.__init__(self)
28+
self.tag = tag
29+
self.sender = sender.FluentSender(tag,
30+
host=host, port=port,
31+
timeout=timeout, verbose=verbose)
1832
self.hostname = socket.gethostname()
1933
self.fsencoding = sys.getfilesystemencoding()
2034
self.encoding = encoding
2135

22-
def format(self, record):
36+
def emit(self, record):
37+
self.sender.emit_with_time(None, record.created, self._build_structure(record))
38+
39+
def _close(self):
40+
self.sender._close()
41+
42+
def _build_structure(self, record):
2343
data = {
2444
u'time': datetime.fromtimestamp(record.created).isoformat(),
2545
u'sys_msecs': record.msecs,
@@ -36,7 +56,7 @@ def format(self, record):
3656
u'sys_processname': self._decode(record.processName),
3757
u'sys_thread': record.thread,
3858
u'sys_threadname': self._decode(record.threadName),
39-
u'message': self._decode(self._format_message(record.msg, record.args))
59+
u'message': self._decode(self.format(record))
4060
}
4161
return data
4262

@@ -70,15 +90,6 @@ def _asciidecode(self, value):
7090
else:
7191
return self._asciidecode(str(value))
7292

73-
def _format_message(self, msg, args):
74-
if isinstance(msg, basestring):
75-
if args:
76-
return msg % args
77-
else:
78-
return msg
79-
else:
80-
return msg
81-
8293
def _format_exception(self, exc_info):
8394
if exc_info is not None and exc_info[0] is not None:
8495
return {
@@ -89,28 +100,4 @@ def _format_exception(self, exc_info):
89100
else:
90101
return None
91102

92-
class FluentHandler(logging.Handler):
93-
'''
94-
Logging Handler for fluent.
95-
'''
96-
def __init__(self,
97-
tag,
98-
host='localhost',
99-
port=24224,
100-
timeout=3.0,
101-
verbose=False):
102-
103-
self.tag = tag
104-
self.sender = sender.FluentSender(tag,
105-
host=host, port=port,
106-
timeout=timeout, verbose=verbose)
107-
self.fmt = FluentRecordFormatter()
108-
logging.Handler.__init__(self)
109-
110-
def emit(self, record):
111-
if record.levelno < self.level: return
112-
data = self.fmt.format(record)
113-
self.sender.emit_with_time(None, record.created, data)
114103

115-
def _close(self):
116-
self.sender._close()

0 commit comments

Comments
 (0)
0