simplistix / errorhandler

This is a handler for the python standard logging framework that can be used to tell whether messages have been logged at or above a certain level.
5 stars 2 forks source link

errorhandler disables default logging output to stderr #1

Closed dhimmel closed 7 years ago

dhimmel commented 7 years ago

Great package! It does exactly what I needed. This repo took me a while to find, but it's an honor to be the first user to star it! The use case it solves seems like it should be pretty common, especially with the growing use of continuous integration builds. I answered a StackOverflow question with this package.

I'm adding errorhandler to our project in https://github.com/greenelab/manubot/pull/2. One thing that confused me at first was that adding error handler suppressed the default logging to stderr. I'm not sure whether this is intentional or a bug. Anyways, here's the code I used to get the stderr logging back:

error_handler = errorhandler.ErrorHandler()
logger = logging.getLogger()
logger.setLevel(getattr(logging, args.log_level))
stream_handler = logging.StreamHandler(stream=sys.stderr)
logger.addHandler(stream_handler)

Is this the simplest workaround?

cjw296 commented 7 years ago

I can assure you errorhandler doesn't do anything of the sort, check the code, there's not much to it:

https://github.com/Simplistix/errorhandler/blob/master/errorhandler/__init__.py

So, having had a look at greenelab/manubot#2, you're hitting the confusing behaviour of the Python logging library as documented here:

https://docs.python.org/2/library/logging.html#logging.basicConfig

Previously, you were doing no setup, so your first call to logging.(error|warning|etc) called basicConfig, but that does nothing as once ErrorHandler() is installed, the root logger already has a handler.

Moral of the story: always explicitly configure your log handlers ;-)

dhimmel commented 7 years ago

Moral of the story: always explicitly configure your log handlers ;-)

Thanks. Just to make sure I understand the recommendation, it's fine to call logging.warning() directly in modules to log to the root logger. However, for functions / scripts that run a program, they should explicitly configure how the root logger is handled?

cjw296 commented 7 years ago

Fine to call logging.warning, but make sure you configure handlers first ;-)

dhimmel commented 7 years ago

Thanks @cjw296. Keep up the great work!