Skip to content

Instantly share code, notes, and snippets.

@lockefox
Last active August 31, 2019 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lockefox/8c537bd75e85014817653744fff0bbac to your computer and use it in GitHub Desktop.
Save lockefox/8c537bd75e85014817653744fff0bbac to your computer and use it in GitHub Desktop.
Everything You Never Wanted To Know About Python Logging (1)

Everything You Never Wanted To Know About Python Logging

print() can't remain the only tool for debugging/reporting in programs. Python offers an extremely powerful suite of reporting tools with the logging library. But be warned, logging isn't merely "print() on steroids", and mishandling this library can become a major pain point.

In this guide we will discuss the underlying model behind logging and how to get the most out of the reporting in our Python projects.

The Life of a Logging Message

TODO: grafix

Drop the idea that logger is just an object that prints messages. Instead, think of logging as an event stream, like Kafka. Let's trace the process from message to log output.

import logging

logger = logging.getLogger('logging_demo')
def main():
    logger.info('hello world!  I'm %s', __file__)

1. Creation of a LogRecord

Every time you call your logger object, a LogRecord is created. This contains both the message, along with metadata. Ever field defined in Formatting is collected at this instant.

TODO: LogRecord fields

Though this means we can print parsable information using StreamHandler or FileHandler, there's also the ability to just bulk upload ALL THE DATA to a service like Graylog or Splunk.

2. Logging Handlers

TODO: grafix - handlers

After a LogRecord is created, it is passed along all the registered loggers. Loggers, by themselves, cannot do anything with LogRecords. Handlers, once attached to a logger, can then emit the message as they see fit.

For StreamHandler and FileHandler, log messages will be combined with a Format String, then written out to a stream or file. Whereas tools like SplunkHandler wil process the LogRecord directly and ignore Formatters entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment