Skip to content

Commit

Permalink
Make log level configurable and update dev and prod defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
jreed1701 committed Mar 29, 2024
1 parent 0d008f1 commit f54a0a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions application/config/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import platform
import uuid
Expand All @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions application/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]:
Expand All @@ -91,15 +91,14 @@ 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(
log_file_full_path,
mode="a",
encoding="utf-8",
maxBytes=constants.DEFAULT_LOG_SIZE_BYTES,
backupCount=5,
backupCount=10,
)
file_handler.setLevel(logger_level)
file_handler.setFormatter(formatter)
Expand All @@ -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,
Expand Down Expand Up @@ -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.")

Expand Down
7 changes: 5 additions & 2 deletions application/gui/launch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import time

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,6 +44,7 @@ def apply(self):

config.DEBUG = True
config.ENV = "development"
config.LOG_LEVEL = logging.DEBUG

self.app = create_app(config=config)

Expand Down

0 comments on commit f54a0a6

Please sign in to comment.