Search code examples
pythonloggingpylonssentryraven

Two Pylons logger handlers (Sentry/Raven and console) for the same qualname


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?


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.