Question

I have a Pylons/TurboGears app. I would like to log the same logger (as specified by the qualname property) to use two different log handlers, each with their own log level.

The Sentry / Raven logger should receive only WARN+ level SQLAlchemy messages, and the console logger should receive INFO+ level SQLAlchemy messages.

Here's my abbreviated ini file:

[loggers]
keys = root, sqlalchemy_console, sqlalchemy_sentry

[handlers]
keys = console, sentry

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console, sentry

[logger_sqlalchemy_console]
level = INFO
handlers = console
qualname = sqlalchemy.engine
propagate = 0

[logger_sqlalchemy_sentry]
level = WARN
handlers = sentry
qualname = sqlalchemy.engine
propagate = 0

However, the logger_sqlalchemy_sentry seems to override logger_sqlalchemy_console and steal its messages. This occurs regardless of the order of loggers in the ini file.

Is it possible using Pylons to log the same logger/qualname to multiple places with different levels?

If so, is it possible for Sentry/Raven to be one of those loggers? Is there something wrong with my ini file, or is there a bug in Raven?

Was it helpful?

Solution

The problem you're having is that you're configuring the sqlalchemy.engine Logger twice. The logger sections correspond to instances of logging.Logger, things that are returned by logging.getLogger(qualname). Only one object can be returned by that call, you can't possibly set up more than one of them with the same qualname.

What you need is multiple handlers for that logger, in the same way that you gave your root logger multiple handlers. You can then specify the desired log level on the individual handlers.

Unfortunately, fileConfig() doesn't give you an easy way to configure the same handler with different log levels depending on the logger that originated the record, you'll need to set up duplicate handler sections for both root and the sqlalchemy.engine loggers in order to have different log levels for them.

OTHER TIPS

You're getting loggers and handlers mixed up - as TokenMacGuy says, you need two handlers for the logger named sqlalchemy.engine. Configure the StreamHandler (console) with level INFO and a SentryHandler (sentry) with level WARNING, and the sqlalchemy.engine logger with a level of DEBUG or INFO. Then you should get the desired result. (Even when DEBUG messages are logged, the levels on the handlers will prevent them emitting events which are less than their level.)

Unless you're limited to Python 2.6 or earlier, it's worth considering using the logging.config.dictConfig API in preference to logging.config.fileConfig, if you can. The dictConfig API allows better control over logging configuration than the older fileConfig API, which will not be further developed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top