ulule/python-logstash-formatter

Support setup via config file

Closed this issue · 2 comments

wosc commented

Configuring the formatter via a configuration file (example below) does not work at the moment, since it overrides the constructor and does not keep the original parameters of logging.Formatter (which are "fmt=None, datefmt=None").

I'd propose changig the constructor signature like so:

     def __init__(self,
+                 fmt=None, datefmt=None,
                  source_host=None,
                  extra={},
                  json_cls=None,
                  json_default=_default_json_default):

This won't enable you to configure the logstash-specific options of the Formatter, since the stdlib logging config file format does not support arbitrary arguments for formatters, but at least it makes the Formatter usable (with default settings) for configuring via config file.

What do you think?

Example config file:

[handlers]
keys = syslog

[formatters]
keys = json

[logger_root]
level = INFO
handlers = syslog

[handler_syslog]
class = logging.handlers.SysLogHandler
args = ()
formatter = json

[formatter_json]
class = logstash_formatter.LogstashFormatter

+1. I get this error:

Traceback (most recent call last):
  File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit
    msg = self.format(record)
  File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format
    return fmt.format(record)
  File "/opt/mail_cannon/lib/python2.6/site-packages/logstash_formatter/__init__.py", line 83, in format
    logr = self.defaults.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

using this config:

[loggers]
keys=root,routing

[handlers]
keys=fileHandler

[formatters]
keys=defaultFormatter

[logger_root]
level=DEBUG
handlers=fileHandler

[logger_routing]
level=DEBUG
handlers=fileHandler
qualname=routing
propagate=0

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=defaultFormatter
args=("logs/lamson.log",)

[formatter_defaultFormatter]
class=logstash_formatter.LogstashFormatter

implementing the one-line fix suggested by @wosc resolves the issue and results in beautiful JSON logs.

pyr commented

Hi,

Thanks for the suggestion, next time, do not hesitate to open a PR.

Cheers,