From 5cac6ffe4051bd0b55e7be07282ecd6a23498729 Mon Sep 17 00:00:00 2001 From: Ruihao Li Date: Mon, 20 Jan 2025 22:21:28 -0500 Subject: [PATCH] all changes related to logging configurations in log_config.yaml based on code review --- application/__init__.py | 9 ++++++--- application/paths.py | 2 ++ log_config.yaml | 14 +++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/application/__init__.py b/application/__init__.py index 0b467c2..de0f21b 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -21,24 +21,27 @@ import os import sys +import application.paths import yaml from flask import Flask, Blueprint, jsonify from werkzeug.exceptions import HTTPException from werkzeug.serving import WSGIRequestHandler -logger = logging.getLogger(__name__) - try: with open('log_config.yaml') as config_file: config = yaml.safe_load(config_file.read()) - os.makedirs(os.path.join(os.getcwd(), 'logs'), exist_ok=True) + config['handlers']['timedRotatingFile']['filename'] = paths.LOG_FILE_PATH + paths.makedir_if_not_exists(paths.LOGS_DIR_PATH) logging.config.dictConfig(config) except Exception: # Fallback to a basic configuration logging.basicConfig(format='%(asctime)s %(levelname)s [%(name)s:%(lineno)d] %(message)s', level=logging.INFO, force=True) + + logger = logging.getLogger(__name__) logger.exception("Logging setup failed") else: + logger = logging.getLogger(__name__) logger.warning("Logging setup is completed with config=%s", config) from .Profile.Profile import Profile diff --git a/application/paths.py b/application/paths.py index 4148177..473d518 100644 --- a/application/paths.py +++ b/application/paths.py @@ -39,3 +39,5 @@ def makedir_if_not_exists(path): USER_PROFILE_PATH = os.path.join(PROFILE_PATH, "user_profile.json") PRIVATE_KEY_PATH = os.path.join(PROFILE_PATH, "private_keys") makedir_if_not_exists(PRIVATE_KEY_PATH) +LOGS_DIR_PATH = os.path.join(PROFILE_PATH, 'logs') +LOG_FILE_PATH = os.path.join(LOGS_DIR_PATH, 'ictrl_log.log') diff --git a/log_config.yaml b/log_config.yaml index f13e6d9..c863d03 100644 --- a/log_config.yaml +++ b/log_config.yaml @@ -1,5 +1,5 @@ version: 1 -disable_existing_loggers: False +disable_existing_loggers: True formatters: default: @@ -12,15 +12,15 @@ handlers: level: DEBUG formatter: default stream: ext://sys.stderr - file_rotating: + timedRotatingFile: class: logging.handlers.TimedRotatingFileHandler level: DEBUG - filename: logs/ictrl_log.log formatter: default - when: 'midnight' - interval: 1 - backupCount: 14 + when: H + interval: 3 + backupCount: 112 + # filename: dynamically assigned upon the initialization of the program root: level: DEBUG - handlers: [console, file_rotating] + handlers: [console, timedRotatingFile]