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:
parent
89994c1f0a
commit
4deeda0964
|
@ -12,25 +12,17 @@ from pathlib import Path
|
||||||
import argparse, textwrap, logging
|
import argparse, textwrap, logging
|
||||||
from alert_processor import AlertProcessor
|
from alert_processor import AlertProcessor
|
||||||
|
|
||||||
#Setup basic logging.
|
|
||||||
log_format='[%(levelname)s] %(asctime)s %(message)s'
|
log_format='[%(levelname)s] %(asctime)s %(message)s'
|
||||||
logging.basicConfig(format=log_format, level=logging.INFO)
|
logging.basicConfig(format=log_format, level=logging.INFO)
|
||||||
logger = logging.getLogger()
|
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():
|
def parse_args():
|
||||||
"""Parse command line arguments."""
|
"""Parse command line arguments."""
|
||||||
parser = argparse.ArgumentParser(description="A simple calendar alerting daemon written in Python")
|
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('--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')
|
parser.add_argument('--loglevel', choices=['info', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Set the logging level')
|
||||||
args = parser.parse_args()
|
return 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
|
|
||||||
|
|
||||||
def read_file(filename):
|
def read_file(filename):
|
||||||
try:
|
try:
|
||||||
|
@ -355,8 +347,20 @@ def process_alert(current_time, next_alert, next_event, event, config):
|
||||||
def main():
|
def main():
|
||||||
# Parse args and config
|
# Parse args and config
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
content = read_file(args.config)
|
if args.logfile != "none":
|
||||||
config = parse_toml(content)
|
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
|
# Get calendar dir
|
||||||
cal_dir = Path(config["app"]["calendar_dir"])
|
cal_dir = Path(config["app"]["calendar_dir"])
|
||||||
|
|
|
@ -8,7 +8,7 @@ Type=simple
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=1
|
RestartSec=1
|
||||||
User=root
|
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"
|
Environment="PYTHONUNBUFFERED=1"
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
|
|
Loading…
Reference in New Issue