From 4deeda09640170d82eaaf6b4ee0002993b83024b Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 15 Feb 2024 18:46:33 +0000 Subject: [PATCH] Update log file path and add logging to service - Updated the `parse_args()` function to include a new argument `--logfile`, which allows users to specify a custom log file. The default value is "none". - Modified the `main()` function to check if the user provided a log file using the `--logfile` argument. If a log file is specified, it adds a FileHandler to the logger and sets the logging level based on the `--loglevel` argument. - Updated the `remindme_caldav.service` file to include the `--logfile` argument in the `ExecStart` command. This ensures that the log file is used when the service starts. --- remindme_caldav.py | 28 ++++++++++++++++------------ remindme_caldav.service | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/remindme_caldav.py b/remindme_caldav.py index eb17583..86dbd00 100644 --- a/remindme_caldav.py +++ b/remindme_caldav.py @@ -12,25 +12,17 @@ from pathlib import Path import argparse, textwrap, logging from alert_processor import AlertProcessor -#Setup basic logging. log_format='[%(levelname)s] %(asctime)s %(message)s' logging.basicConfig(format=log_format, level=logging.INFO) logger = logging.getLogger() -file_handler = logging.FileHandler('log', mode='a') -formatter = logging.Formatter(log_format) -file_handler.setFormatter(formatter) -logger.addHandler(file_handler) def parse_args(): """Parse command line arguments.""" parser = argparse.ArgumentParser(description="A simple calendar alerting daemon written in Python") parser.add_argument('--config', type=str, help='Path to config file. Must be .toml') + parser.add_argument('--logfile', type=str, help='Path to logfile file. Default logfile', default = "none") parser.add_argument('--loglevel', choices=['info', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Set the logging level') - args = parser.parse_args() - if args.config is None: - logger.error("No config file provided. Please use --config path_to_config.toml") - sys.exit(1) - return args + return parser.parse_args() def read_file(filename): try: @@ -355,8 +347,20 @@ def process_alert(current_time, next_alert, next_event, event, config): def main(): # Parse args and config args = parse_args() - content = read_file(args.config) - config = parse_toml(content) + if args.logfile != "none": + file_handler = logging.FileHandler(args.logfile, mode='a') + formatter = logging.Formatter(log_format) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + + if args.config is None: + logger.error("No config file provided. Please use --config path_to_config.toml") + sys.exit(1) + + config_file = read_file(args.config) + config = parse_toml(config_file) + + # Write logs to logfile # Get calendar dir cal_dir = Path(config["app"]["calendar_dir"]) diff --git a/remindme_caldav.service b/remindme_caldav.service index 1b78bce..98321a1 100644 --- a/remindme_caldav.service +++ b/remindme_caldav.service @@ -8,7 +8,7 @@ Type=simple Restart=always RestartSec=1 User=root -ExecStart=/opt/remindme_caldav/.venv/bin/python3 -u /opt/remindme_caldav/remindme_caldav.py --config /etc/remindme_caldav/config.toml +ExecStart=/opt/remindme_caldav/.venv/bin/python3 -u /opt/remindme_caldav/remindme_caldav.py --config /etc/remindme_caldav/config.toml --logfile /opt/remindme_caldav/log Environment="PYTHONUNBUFFERED=1" [Install]