JSON Logging for Sanic in python

JSON Logging for Sanic

The other day I was running some containers on Amazon’s ECS and logging to cloudwatch. I then learnt cloudwatch parses JSON logs so obviously I then wanted Sanic to log out JSON.

Ideally this’ll be useful to people but if it isn’t, raise an issue and we’ll make it better 🙂

To install:

pip install sanic-json-logging

Look at examples/simple.py for a full working example, but this will essentially get you going

import sanic
from sanic_json_logging import setup_json_logging

app = sanic.Sanic(name="somename")
setup_json_logging(app)

setup_json_logging does the following:

  • changes the default log formatters to JSON ones
  • also filters out no Keepalive warnings
  • unless told otherwise, will change the asyncio task factory, to implement some rudimentary task-local storage.
  • installs pre and post request middleware. Pre-request middleware to time tasks and generate a uuid4 request id. Post-request middleware

     

     

     

    To finish reading, please visit source site