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.
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__)
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.
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 Formatter
s entirely.