ifschleife
ifschleife

Reputation: 1065

Logging problems when using modules individually

I'm making extensive use of the logging module in all my own modules. This works fine when I create a configuration for the logging module in my main routine. But when I want to test one of my modules individually in an interactive shell and logging.getLogger("foo") is executed I get the error:

No handlers could be found for logger "foo"

This makes sense of course because the logging module hasn't been configured yet. When I just call logging.basicConfig() in all my modules again my logs will be printed more than once (the python doc also says to only call basicConfig() once in the main thread).

So, how is this done cleanly? Can I check if the logging module has already been configured?

Upvotes: 1

Views: 184

Answers (1)

Nicola Musatti
Nicola Musatti

Reputation: 18236

It is explained in the Logging module How-To. You could create a helper module to be included wherever you perform any logging:

import logging
logging.getLogger('your_top_level_package').addHandler(logging.NullHandler())

Upvotes: 1

Related Questions