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.
This commit is contained in:
Sam 2024-02-15 18:46:33 +00:00
parent 89994c1f0a
commit 4deeda0964
2 changed files with 17 additions and 13 deletions

View File

@ -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"])

View File

@ -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]