Notes about python logging

By | January 12, 2021

Consider the following code

Executing above:

  1. It won't log anything to console, because it default to level warning
  2. It won't log anything to console, because no stream has been set

Executing above:

  1. Running basicConfig(...) default adds the stream to stderr (unless specified explicitly) to root logger
  2. It sets the root logger level to INFO
  3. Log being sent to console by the root logger
  4. Calling basicConfig again is no-op because it's already configured, in python 3.8 you can pass in force=True to override the config.

Executing above:

  1. It sets the level to the root logger
  2. The log instance inherited from the root logger
  3. We can see the logs sent to console by the log instance

Executing above:

  1. The log instance inherited from the root logger, notice basicConfig has not been called yet
  2. The root logger calls basicConfig
  3. Only the log from root logger being sent to console

Ensure events are not logged before the logging system has been configured. Usually this means avoiding log events or doing any work at the global (module level) scope.

https://stackoverflow.com/questions/65676510/calling-logging-interferes-other-modules

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.