When Python logging isn’t

Python’s logging framework has a quirk. By default it will log any message that is warning or higher to stderr. The framework provides a function, basicConfig(), that can be used to tweak the format, where logs are sent, and what levels should be logged.

The quirk that will get you eventually, though, is once something has been logged, calling basicConfig() does nothing.

A quick way to reset logging is to check if logging.root is defined, and clear it out any handlers if it is:


if logging.root:
del logging.root.handlers[:]

After cleaning out the root handlers, calling basicConfig() works again.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s