#133 fix triplicated log messages
Merged 4 years ago by kparal. Opened 4 years ago by kparal.

file modified
+2 -4
@@ -82,6 +82,8 @@ 

  def setup_logging():

      root_logger = logging.getLogger('')

      root_logger.setLevel(logging.DEBUG)

+     root_logger.handlers.clear()

+     app.logger.handlers.clear()

  

      if app.config['STREAM_LOGGING']:

          app.logger.debug("doing stream logging")
@@ -89,7 +91,6 @@ 

          stream_handler.setLevel(loglevel)

          stream_handler.setFormatter(formatter)

          root_logger.addHandler(stream_handler)

-         app.logger.addHandler(stream_handler)

  

      if app.config['SYSLOG_LOGGING']:

          app.logger.debug("doing syslog logging")
@@ -98,7 +99,6 @@ 

          syslog_handler.setLevel(loglevel)

          syslog_handler.setFormatter(formatter)

          root_logger.addHandler(syslog_handler)

-         app.logger.addHandler(syslog_handler)

  

      if app.config['FILE_LOGGING'] and app.config['LOGFILE']:

          app.logger.debug("doing file logging to %s" % app.config['LOGFILE'])
@@ -106,8 +106,6 @@ 

          file_handler.setLevel(loglevel)

          file_handler.setFormatter(formatter)

          root_logger.addHandler(file_handler)

-         app.logger.addHandler(file_handler)

- 

  

  setup_logging()

  

Before this patch, all messages logged with logging.getLogger('foo') were
printed once, but all messages logged with app.logger were printed triplicated.
The reason was that
a) the app.logger is initialized with stream logging enabled by default, so
we were adding it the second time
b) everything sent to the app.logger goes also through the root_logger which
was also configured with the same handlers, which resulted in one extra message

The fix is to clean all handlers on program start, so that default handlers are
removed. And then only add handlers to the root_logger and not also to the
app.logger, because the message passes both when app.logger is invoked.

Seems okay to me, thanks.

Maybe let's get ack from @jskladan ?

Commit 9ba3722 fixes this pull-request

Pull-Request has been merged by kparal

4 years ago
Metadata