ulule/python-logstash-formatter

LogstashFormatterV1: handling msg as dict

Closed this issue · 0 comments

LogstashFormatterV1 does not support dict messages. Code from Readme will raise an exception:

import logging
from logstash_formatter import LogstashFormatterV1

logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = LogstashFormatterV1()

handler.setFormatter(formatter)
logger.addHandler(handler)

logger.error({"account": 123, "ip": "172.20.19.18"})
--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py", line 978, in emit
    msg = self.format(record)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py", line 828, in format
    return fmt.format(record)
  File "/usr/local/lib/python3.4/site-packages/logstash_formatter/__init__.py", line 142, in format
    msg = msg.format(**fields)
AttributeError: 'dict' object has no attribute 'format'
Call stack:
  File "/Users/nikitaprianichnikov/Sources/scraper-ng/temp/foo.py", line 12, in <module>
    logger.error({"account": 123, "ip": "172.20.19.18"})
Message: {'ip': '172.20.19.18', 'account': 123}
Arguments: ()