From 6a9f1886c70658636f36272ea8b8f4c3f5f4a7a9 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Fri, 12 Nov 2021 06:09:32 +0400 Subject: Disable File Logs In Production The most recent changes to our log setup had the loggers writing to a read-only location in prod. This would cause an error during startup. To get around this while keeping the change, I moved it to only be used if debug is True. Signed-off-by: Hassan Abouelela --- bot/log.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'bot/log.py') diff --git a/bot/log.py b/bot/log.py index 5e0e909d..97561be4 100644 --- a/bot/log.py +++ b/bot/log.py @@ -18,19 +18,22 @@ def setup() -> None: format_string = "%(asctime)s | %(name)s | %(levelname)s | %(message)s" log_format = logging.Formatter(format_string) + root_logger = logging.getLogger() - # Set up file logging - log_file = Path("logs/sir-lancebot.log") - log_file.parent.mkdir(exist_ok=True) + # Copied from constants file, which we can't import yet since loggers aren't instantiated + debug = os.environ.get("BOT_DEBUG", "true").lower() == "true" - # File handler rotates logs every 5 MB - file_handler = logging.handlers.RotatingFileHandler( - log_file, maxBytes=5 * (2 ** 20), backupCount=10, encoding="utf-8", - ) - file_handler.setFormatter(log_format) + if debug: + # Set up file logging + log_file = Path("logs/sir-lancebot.log") + log_file.parent.mkdir(exist_ok=True) - root_logger = logging.getLogger() - root_logger.addHandler(file_handler) + # File handler rotates logs every 5 MB + file_handler = logging.handlers.RotatingFileHandler( + log_file, maxBytes=5 * (2 ** 20), backupCount=10, encoding="utf-8", + ) + file_handler.setFormatter(log_format) + root_logger.addHandler(file_handler) if "COLOREDLOGS_LEVEL_STYLES" not in os.environ: coloredlogs.DEFAULT_LEVEL_STYLES = { -- cgit v1.2.3