Python logging set up

Posted on Mon 05 February 2018 in misc

Quick overview of our boilerplate for logging

creds is a helper file which returns to us our api keys, logings etc from secure storage (reads from config files)

import logging
import graypy

log = logging.getLogger(creds['logstash']['facility'])

graylog = graypy.GELFHandler(creds['logstash']['host'], creds['logstash']['port'])
console = logging.StreamHandler()


formatter = logging.Formatter(creds['logstash']['formatter'])


The graypy module alows us to send GELF messages to a Graylog server. This is a great way of storing all log files in a central location. I highly recommend.

We use the stream handler to output messages to the console, this really helps in debugging apps

From the top:

  • Import logging, and create a log instance
  • Set a default logging level - here I have used DEBUG
  • Set up our handlers , graylog and consoler
  • Build a formatter, usinging a pre-determined config , using this, we can change the formatting wihtout changing the code
  • Apply the formatter to both handlers
  • apply the handlers to the log

The outcome of this is we can info messages logged to graylog, and the more verbose output to the screen, which we can use when debugging apps in production and running them manually.