From 1905de1eac22a2557861e9f5ca01c0f19420cd33 Mon Sep 17 00:00:00 2001 From: Ruihao Li Date: Sat, 18 Jan 2025 18:55:37 -0500 Subject: [PATCH 1/3] Addition of timedRotatingHandler to log_config.yaml for messages to be logged in files --- log_config.yaml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/log_config.yaml b/log_config.yaml index 672af5a..07c4615 100644 --- a/log_config.yaml +++ b/log_config.yaml @@ -1,5 +1,5 @@ version: 1 -disable_existing_loggers: True +disable_existing_loggers: False formatters: default: @@ -12,7 +12,15 @@ handlers: level: DEBUG formatter: default stream: ext://sys.stderr + file_rotating: + class: logging.handlers.TimedRotatingFileHandler + level: DEBUG + filename: ictrl_log.log + formatter: default + when: 'midnight' + interval: 1 + backupCount: 14 root: level: DEBUG - handlers: [console] + handlers: [console, file_rotating] From 0c148d0e47dc9ae42014f10a4a04e0aaffca7cbd Mon Sep 17 00:00:00 2001 From: Ruihao Li Date: Sun, 19 Jan 2025 01:25:32 -0500 Subject: [PATCH 2/3] Direct all log files to the logs directory --- application/__init__.py | 2 ++ log_config.yaml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/application/__init__.py b/application/__init__.py index 943df74..0b467c2 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -31,6 +31,8 @@ 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) logging.config.dictConfig(config) except Exception: # Fallback to a basic configuration diff --git a/log_config.yaml b/log_config.yaml index 07c4615..f13e6d9 100644 --- a/log_config.yaml +++ b/log_config.yaml @@ -15,7 +15,7 @@ handlers: file_rotating: class: logging.handlers.TimedRotatingFileHandler level: DEBUG - filename: ictrl_log.log + filename: logs/ictrl_log.log formatter: default when: 'midnight' interval: 1 From 5cac6ffe4051bd0b55e7be07282ecd6a23498729 Mon Sep 17 00:00:00 2001 From: Ruihao Li Date: Mon, 20 Jan 2025 22:21:28 -0500 Subject: [PATCH 3/3] 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]