Hello,
I am trying to build my own logbook handler that sends logs to a logstash
server. My code is along these lines (g_logstash_pusher class omitted for
brevity):
class LogstashHandler(logbook.Handler, logbook.StringFormatterHandlerMixin):
formatter_class = PasswordHidingFormatter
def __init__(self, level=logbook.NOTSET, format_string=None,
encoding=None, filter=None, bubble=True):
logbook.Handler.__init__(self, level, filter, bubble)
logbook.StringFormatterHandlerMixin.__init__(self, format_string)
def emit(self, record):
d = dict(LOGSTASH_EXTRA_FIELDS)
d.update(record.extra)
for attr in ['message', 'func_name', 'module', 'filename', 'lineno',
'thread', 'thread_name',
'formatted_exception', 'exception_name']:
value = getattr(record, attr)
if value:
d[attr] = value
print ">>>>>>>>>>>>>>>>", repr(record.message)
d['levelname'] = getattr(record, 'level_name')
msg = json.dumps(d)
print msg
if g_logstash_pusher is not None:
g_logstash_pusher.queue_message(msg)
[...]
FORMAT_STRING = '{record.time:%Y-%m-%d %H:%M:%S.%f} {record.channel}
{record.level_name:5s} {record.message}'
logstash_handler = LogstashHandler(
level=getattr(logbook, level),
format_string=FORMAT_STRING)
logstash_handler.push_application()
This is working fine for most message, but some other messages this outputs
something like:
Traceback (most recent call last):
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 214, in handle
self.emit(record)
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 849, in emit
self.write(self.format_and_encode(record))
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 617, in format_and_encode
return StreamHandler.format_and_encode(self, record)
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 549, in format_and_encode
rv = self.format(record) + '\n'
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 193, in format
return self.formatter(record, self)
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 374, in __call__
line = self.format_record(record, handler)
File
"/usr/local/lib/python2.7/dist-packages/Logbook-0.6.1_dev_20131024-py2.7.egg/logbook/handlers.py",
line 358, in format_record
return self._formatter.format(record=record, handler=handler)
KeyError: 'message'
Any idea why the 'message' attribute seems to disappear from the log record
once in a while? If I remove my handler, logging is fine. In fact, in my
handler everything is fine, but the next handler in the stack cannot read
record.message for some reason. Do you think the LogRecord object is
being mutated in some way to make record.message inaccessible?
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/groups/opt_out.