diff --git a/application/__init__.py b/application/__init__.py index 943df74..8a32091 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -21,22 +21,26 @@ 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()) + + config['handlers']['timedRotatingFile']['filename'] = paths.LOG_FILE_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..797e295 100644 --- a/application/paths.py +++ b/application/paths.py @@ -39,3 +39,6 @@ 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') +makedir_if_not_exists(LOGS_DIR_PATH) +LOG_FILE_PATH = os.path.join(LOGS_DIR_PATH, 'ictrl_log.log') diff --git a/log_config.yaml b/log_config.yaml index 672af5a..d0da5a8 100644 --- a/log_config.yaml +++ b/log_config.yaml @@ -12,7 +12,16 @@ handlers: level: DEBUG formatter: default stream: ext://sys.stderr + timedRotatingFile: + class: logging.handlers.TimedRotatingFileHandler + level: DEBUG + formatter: default + when: H + interval: 3 + # See discussion on backupCount decision: https://github.com/junhaoliao/iCtrl/pull/46/files/0c148d0e47dc9ae42014f10a4a04e0aaffca7cbd#r1921487166 + backupCount: 112 + # filename: To be dynamically assigned upon application initialization. root: level: DEBUG - handlers: [console] + handlers: [console, timedRotatingFile]