brgd.eu

Doing Logging in a Python Library

2023-08-17

Today I looked up how to configure logging in a Python library. There is a section on this in the Python tutorials, or also a slightly shorter summary here.

Some main takeaways are:

  • Don't use a root logger. Instead, create loggers using the __name__ global variable, to make the library logger unique.
  • Don't use any handlers. Instead, let the user decide on that and only place the NullHandler.

One can place the NullHandler like this:

import logging

logging.getLogger(__name__).addHandler(logging.NullHandler())