From f54a0a61764794b6b23ffc24b6fa797b3625cdf0 Mon Sep 17 00:00:00 2001 From: Joshua Reed <11220408+jreed1701@users.noreply.github.com> Date: Fri, 29 Mar 2024 10:59:29 -0600 Subject: [PATCH] Make log level configurable and update dev and prod defaults. --- application/config/config.py | 2 ++ application/factory.py | 9 ++++----- application/gui/launch.py | 7 +++++-- server.py | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/application/config/config.py b/application/config/config.py index ee9be5d..579b3c1 100644 --- a/application/config/config.py +++ b/application/config/config.py @@ -1,3 +1,4 @@ +import logging import os import platform import uuid @@ -19,6 +20,7 @@ class DefaultConfig: FLASK_RUN_PORT = "5000" FLASK_FORCE_AUTH = False # Leave as False except in testing. FLASK_DISABLE_AUTH = False + LOG_LEVEL = logging.NOTSET # NGINX Settings NGINX_DEFAULT_HOSTNAME = "localhost" diff --git a/application/factory.py b/application/factory.py index 4e2e6ba..8708449 100644 --- a/application/factory.py +++ b/application/factory.py @@ -69,7 +69,7 @@ def _handle_migrations(flask_app: Flask): command.upgrade(alembic_cfg, "head") -def _handle_logging(): +def _handle_logging(logger_level=constants.DEFAULT_LOG_LEVEL): """Update log configuration.""" # Remove all handlers associated with the root logger object. for handler in logging.root.handlers[:]: @@ -91,7 +91,6 @@ def _handle_logging(): # Reconfigure logging again, this time with a file in addition to stdout. formatter = logging.Formatter(constants.DEFAULT_LOG_FORMAT) - logger_level = constants.DEFAULT_LOG_LEVEL # File handler file_handler = ConcurrentRotatingFileHandler( @@ -99,7 +98,7 @@ def _handle_logging(): mode="a", encoding="utf-8", maxBytes=constants.DEFAULT_LOG_SIZE_BYTES, - backupCount=5, + backupCount=10, ) file_handler.setLevel(logger_level) file_handler.setFormatter(formatter) @@ -116,7 +115,7 @@ def _handle_logging(): def create_app(config=None): if config is None: config = DefaultConfig("python") - logger.info("WARNING. Missing Configuration. Initializing with default...") + logger.warning("WARNING. Missing Configuration. Initializing with default...") flask_app = Flask( constants.APP_NAME, @@ -179,7 +178,7 @@ def create_app(config=None): # Run other startup checks. _startup_checks() - _handle_logging() + _handle_logging(logger_level=config.LOG_LEVEL) logger.info(f"{constants.APP_NAME} has been successfully created.") diff --git a/application/gui/launch.py b/application/gui/launch.py index c925d74..35c3564 100644 --- a/application/gui/launch.py +++ b/application/gui/launch.py @@ -1,3 +1,4 @@ +import logging import os import time @@ -54,9 +55,11 @@ def _create_backend(self) -> Flask: config = DefaultConfig("python") config.obtain_environment_variables() + config.DEBUG = False + config.LOG_LEVEL = logging.INFO + config.ENV = "production" + app = create_app(config=config) - app.debug = False - app.config["ENV"] = "production" return app diff --git a/server.py b/server.py index 89d21ee..86b680e 100755 --- a/server.py +++ b/server.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- - import argparse as _argparse +import logging import sys as _sys from application.config.config import DefaultConfig @@ -45,6 +44,7 @@ def apply(self): config.DEBUG = True config.ENV = "development" + config.LOG_LEVEL = logging.DEBUG self.app = create_app(config=config)