Skip to content

Commit

Permalink
Do not instantiate logging handlers twice
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Nov 11, 2024
1 parent 1871019 commit ded8d3d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions common/lib/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,24 @@ def __init__(self, logger_name='4cat-backend', output=False, filename='4cat.log'
self.logger.setLevel(log_level)

# this handler manages the text log files
handler = RotatingFileHandler(self.log_path, maxBytes=(50 * 1024 * 1024), backupCount=1)
handler.setLevel(log_level)
handler.setFormatter(logging.Formatter("%(asctime)-15s | %(levelname)s at %(location)s: %(message)s",
"%d-%m-%Y %H:%M:%S"))
self.logger.addHandler(handler)

# the slack webhook has its own handler, and is only active if the
# webhook URL is set
try:
if config.get("logging.slack.webhook"):
slack_handler = SlackLogHandler(config.get("logging.slack.webhook"))
slack_handler.setLevel(self.levels.get(config.get("logging.slack.level"), self.alert_level))
self.logger.addHandler(slack_handler)
except Exception:
# we *may* need the logger before the database is in working order
if config.db is not None:
config.db.rollback()
if not self.logger.handlers:
handler = RotatingFileHandler(self.log_path, maxBytes=(50 * 1024 * 1024), backupCount=1)
handler.setLevel(log_level)
handler.setFormatter(logging.Formatter("%(asctime)-15s | %(levelname)s at %(location)s: %(message)s",
"%d-%m-%Y %H:%M:%S"))
self.logger.addHandler(handler)

# the slack webhook has its own handler, and is only active if the
# webhook URL is set
try:
if config.get("logging.slack.webhook"):
slack_handler = SlackLogHandler(config.get("logging.slack.webhook"))
slack_handler.setLevel(self.levels.get(config.get("logging.slack.level"), self.alert_level))
self.logger.addHandler(slack_handler)
except Exception:
# we *may* need the logger before the database is in working order
if config.db is not None:
config.db.rollback()

def log(self, message, level=logging.INFO, frame=None):
"""
Expand Down

0 comments on commit ded8d3d

Please sign in to comment.