-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom logger in order to save in file
- Loading branch information
Ronan
committed
Sep 17, 2024
1 parent
410e9e6
commit 59a5e1e
Showing
7 changed files
with
58 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .logger_config import logger # Ensure logger is initialized first | ||
from .core import * | ||
from . import engine, sensors, utils | ||
from .version import __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import logging | ||
import os | ||
from logging.handlers import TimedRotatingFileHandler | ||
|
||
# Define the logging format | ||
log_format = "%(asctime)s | %(levelname)s: %(message)s" | ||
os.makedirs(os.path.dirname("/var/log/engine.log"), exist_ok=True) | ||
|
||
# Create a StreamHandler (for stdout) | ||
stream_handler = logging.StreamHandler() | ||
stream_handler.setFormatter(logging.Formatter(log_format)) | ||
|
||
# Create a TimedRotatingFileHandler (for writing logs to a new file every day) | ||
file_handler = TimedRotatingFileHandler("/var/log/engine.log", when="midnight", interval=1, backupCount=5) | ||
file_handler.setFormatter(logging.Formatter(log_format)) | ||
file_handler.suffix = "%Y-%m-%d" # Adds a date suffix to the log file name | ||
|
||
# Export the logger instance | ||
logger = logging.getLogger() # Get the root logger | ||
|
||
# Ensure we only add handlers once | ||
logger.addHandler(stream_handler) | ||
logger.addHandler(file_handler) | ||
|
||
logger.setLevel(logging.INFO) | ||
logger.info("Logger is set up correctly.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters