A More Pythonic Logging System; or, You Deserve Better Than log4j
The Basic Contract of Logging
Logging should be simple and intuitive.
For most use cases, you want to quickly instantiate a logger and dump some text to a stream. You would expect a common workflow based on a minimum level of verbosity in the log entries and for those entries to be formatted in some fashion that is both human readable and machine parseable. There should also be a set of common metadata that can be used to provide context to the entry.
That context should also be easily extended to suit everyone's use cases. Additionally, the values passed to that context should be pliable - users should have the option to override those values as they deem necessary.
Common meta information should conform to as widely adopted standards as possible - i.e. ISO 8601 timestamps and full unicode supported messages.
As stated foremost, the interface to this system should be simple and intuitive. This means the complexity of the system should be minimized, configuration should have sane defaults and the supporting library should be packed with expressive documentation.
Implementation of the Contract
The user should only be concerned with three components:
- Verbosity (
- Message Producers (
- Message Creation (
logging2 is available through PyPI, and thus can be installed via pip:
$ pip install logging2
Logging should be simple and intuitive. With that in mind, the easiest way to get up and running is
to instantiate a
Logger and start producing entries:
>>> from logging2 import Logger >>> logger = Logger('app') >>> logger.info('Hello, world!') 2017-04-29T17:08:23.156795+00:00 INFO app: Hello, world!
The default logger will dump all log entries to STDOUT with a minimum verbosity of
There are numerous configurations, all with simple and easy to rationalize behavior:
- log entry verbosity
- log producers (handlers)
- intuitive interface to creating log entries (loggers)
Logger s have a handful of ways of creating log entries via:
debugfor the most verbose level of messages
infofor typical informational messages
warningfor calling user attention to a potentially hazardous conditions
errorfor altering users to captured and recovered from error conditions
exceptionfor capturing exception tracebacks in the log
The mechanism for producing the log entries to the output streams is via
Handler s. Handlers
are broken into three groups:
streamingfor common IO messaging (typically STDOUT and STDERR)
filesfor file system based IO
socketsfor network based messaging
All of which are found in the
This logging utility is designed for Python 3.6 and better. It will not be backported to support any earlier versions of Python.