Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raven's SentryHandler is suppressing StreamHandler output to file

I have a plain python (non-Django) project where I'm trying to tie Raven into the logging setup.

Under our current setup, we use a simple logging config:

import logging
logging.basicConfig(format='long detailed format',
                    level=logging.DEBUG)

The output is then redirected to a log file; this produces a nice, verbose log that we can look through when we need to.

We now want to add Raven's error logging, tying it into our current logging setup so that logging.error calls also result in a message being sent to the Sentry server. Using the following code:

from raven import Client
from raven.conf import setup_logging
from raven.handlers.logging import SentryHandler

raven = Client(environ.get('SENTRYURL', ''), site='SITE')
setup_logging(SentryHandler(raven, level=logging.ERROR))

Errors are being successfully sent to Sentry, but I'm now getting only a single line of file output:

DEBUG: Configuring Raven for host: <DSN url>

All other file output -- from logging.debug to logging.error -- is being suppressed.

If I comment the setup_logging line, I get file output but no Sentry errors. What am I doing wrong?

like image 431
eternicode Avatar asked Oct 22 '22 13:10

eternicode


1 Answers

This turned out to be a case of sloppy code. We had some hack elsewhere in the startup execution path that re-initialized the logging:

logging.root.removeHandler(logging.root.handlers[0]) # Undo previous basicConfig
logging.basicConfig(format='same long format',
                    level=logging.DEBUG)

However, since logging.basicConfig doesn't do anything if logging.root has existing handlers, this simply removed the stream handler, leaving the sentry handler, and caused basicConfig to then act as no-op, meaning we lost our StreamHandler altogether.

Removing these lines and having only one basicConfig and a setup_logging call worked.

like image 122
eternicode Avatar answered Oct 27 '22 10:10

eternicode