Package: jsonlog
jsonlogA drop-in formatter for Python'sloggingmodule that outputs messages as line
delimited JSON.Whilejsonlogprovides it's ownbasicConfigmethod so you can get started
quickly, all of it's features and classes can be used with theloggingmodule.UsageYou can usejsonlogas a drop-in replacement forlogging:importjsonlogjsonlog.warning("Hello world."){"timestamp":"2019-06-21T19:06:25.285730","level":"WARNING","name":"root","message":"Hello world."}It's implemented as a log formatter, so you can useloggingjust like you
normally would.importjsonlogimportloggingjsonlog.basicConfig(level=jsonlog.INFO)jsonlog.warning("Works with functions in the jsonlog module.")logging.warning("And works with functions in the logging module.")Configuration usingjsonlog.basicConfigThejsonlog.basicConfigfunction accepts slightly different parameters tologging.basicConfig. It's shown here with the defaults for each parameter.Thefilename,filemodeandstreamparameters work the same way as their
equivalents inlogging.basicConfig, and as suchfilenameandstreamare
exclusive.importjsonlogjsonlog.basicConfig(level=jsonlog.INFO,indent=None,keys=("timestamp","level","message"),timespec="auto",# filename=None,# filemode="a",# stream=None,)Configuration usinglogging.config.dictConfigAny of the configuration methods inlogging.configcan be used to configure a
handler that usesjsonlog.formmatters.JSONFormatterto format records as JSON.importlogging.configlogging.config.dictConfig({"version":1,"formatters":{"json":{"()":"jsonlog.JSONFormatter"}},"handlers":{"stream":{"class":"logging.StreamHandler","formatter":"json"}},"loggers":{"":{"handlers":["stream"]}},})Adding extra attributes to JSON outputRecord attributes provided withextra=will be included in the JSON object.importjsonlogimportloggingjsonlog.basicConfig()logging.warning("User clicked a button",extra={"user":123}){"timestamp":"2019-06-21T19:06:54.293929","level":"WARNING","name":"root","message":"User clicked a button","user":123}If a mapping is passed as the only positional argument, attributes from the
mapping will also be included.importjsonlogimportloggingjsonlog.basicConfig()logging.warning("User clicked a button",{"user":123})PipeliningTry piping logs throughjqif you want to read them on the command line!pythonexamples/hello.py2>>(jq.){"timestamp":"2019-06-21T19:07:43.211782","level":"WARNING","name":"root","message":"Hello world."}TracebacksTracebacks are included as a single string - it's not very nice to read, but
means it'll play nicely with any systems that read the JSON logs you output.{"timestamp":"2019-06-21T19:08:37.326897","level":"ERROR","name":"root","message":"Encountered an error","traceback":"Traceback (most recent call last):\n  File \"examples/error.py\", line 6, in <module>\n    raise ValueError(\"Example exception\")\nValueError: Example exception"}Tools likejqmake it easy to extract and read the traceback:pythonexamples/error.py2>>(jq-r".traceback")Traceback (most recent call last):
  File "examples/error.py", line 6, in <module>
    raise ValueError("Example exception")
ValueError: Example exceptionCompatibilityjsonlogis written for Python 3.7 and above. Compatibility patches will be
accepted for Python 3.5 and above, but patches for Python 2 will be rejected.To usejsonlogon Python 3.6 you will need to install thedataclassespackage alongside it. This isn't a dependency as it breaks the builtindataclassesmodule when installed on Python 3.7 and above.ReferencesBuild for use with theloggingmodule.Partially based oncolorlog.Works great withjq!AuthorsSam Clements