Skip to content

Commit

Permalink
all changes related to logging configurations in log_config.yaml base…
Browse files Browse the repository at this point in the history
…d on code review
  • Loading branch information
li-ruihao committed Jan 21, 2025
1 parent 0c148d0 commit 5cac6ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions application/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
14 changes: 7 additions & 7 deletions log_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: 1
disable_existing_loggers: False
disable_existing_loggers: True

formatters:
default:
Expand All @@ -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]

0 comments on commit 5cac6ff

Please sign in to comment.