Skip to content

Addition of TimedRotatingFileHandler to log_config.yaml for iCtrl's logging configuration #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
import os
import sys

import application.paths
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the import statement to match usage.

The code imports application.paths but later uses paths directly, which will cause an undefined name error.

-import application.paths
+import application.paths as paths
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import application.paths
import application.paths as paths
🧰 Tools
🪛 Ruff (0.8.2)

24-24: application.paths imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

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__)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep Line 29 and remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning for this was explained in PR#48, but I forgot I also made the changes here since these changes were not merged. I can collapse the other PR onto this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider initializing the logger only once.

The logger is initialized in both the exception and else blocks. Consider initializing it once before the try block and reusing it to reduce duplication.

+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)

Also applies to: 43-43

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
3 changes: 3 additions & 0 deletions application/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
11 changes: 10 additions & 1 deletion log_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading