ulule/python-logstash-formatter

TypeError: 'str' object is not callable

Closed this issue · 3 comments

In python 3 with configparser i get the following error when trying to log a message:

Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/__init__.py", line 980, in emit
    msg = self.format(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 830, in format
    return fmt.format(record)
  File "/home/mafm/pycharm-35venv/lib/python3.5/site-packages/logstash_formatter-0.5.15-py3.5.egg/logstash_formatter/__init__.py", line 172, in format
    return json.dumps(logr, default=self.json_default, cls=self.json_cls)
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
TypeError: 'str' object is not callable

It seems that _create_formatters in config.py of 'logging' will add a third argument (style) when initializing logstash formatter. This puts a string ("%") in stead of the json_cls.
Might be a new behaviour of "logging"?

Maybe the fix is as simple as changing the constructor function (init) to

def __init__(self,
                 fmt=None,
                 datefmt=None,
                 style=None,
                 json_cls=None,
                 json_default=_default_json_default):

?

Ok, found this in the logging documentation:
https://docs.python.org/3/library/logging.html#logging.Formatter

class logging.Formatter(fmt=None, datefmt=None, style='%')¶
Returns a new instance of the Formatter class. The instance is initialized with a format string for the message as a whole, as well as a format string for the date/time portion of a message. If no fmt is specified, '%(message)s' is used. If no datefmt is specified, the ISO8601 date format is used.

The style parameter can be one of ‘%’, ‘{‘ or ‘$’ and determines how the format string will be merged with its data: using one of %-formatting, str.format() or string.Template. See Using particular formatting styles throughout your application for more information on using {- and $-formatting for log messages.

Changed in version 3.2: The style parameter was added.

What's the right fix? requiring an older version of logging?

Hi @mads79 - try upgrading to version 0.5.16

Hi @saabeilin -- 0.5.16 works for me 👍
Thanks a lot :) (hope it's ok I close the issue)