From 0a038dae4457b9b7c98cdfb438740baab752f05c Mon Sep 17 00:00:00 2001 From: Hemna Date: Thu, 25 Feb 2021 13:32:50 -0500 Subject: [PATCH] Added log format and dateformat to config file This patch moves the default log format string and date format string to the config file, so users can format the logs as they see fit. The default log format also includes the file and line number that posted the log entry. The new entries in the config are here: aprsd: logformat: "String here" dateformat: "string here" --- aprsd/main.py | 4 ++-- aprsd/utils.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/aprsd/main.py b/aprsd/main.py index d0b621e..2bf476f 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -177,8 +177,8 @@ def signal_handler(sig, frame): def setup_logging(config, loglevel, quiet): log_level = LOG_LEVELS[loglevel] LOG.setLevel(log_level) - log_format = "[%(asctime)s] [%(threadName)-12s] [%(levelname)-5.5s]" " %(message)s" - date_format = "%m/%d/%Y %I:%M:%S %p" + log_format = config["aprsd"].get("logformat", utils.DEFAULT_LOG_FORMAT) + date_format = config["aprsd"].get("dateformat", utils.DEFAULT_DATE_FORMAT) log_formatter = logging.Formatter(fmt=log_format, datefmt=date_format) log_file = config["aprsd"].get("logfile", None) if log_file: diff --git a/aprsd/utils.py b/aprsd/utils.py index 8b84cea..c101fe5 100644 --- a/aprsd/utils.py +++ b/aprsd/utils.py @@ -11,6 +11,13 @@ from aprsd import plugin import click import yaml +DEFAULT_LOG_FORMAT = ( + "[%(asctime)s] [%(threadName)-12s] [%(levelname)-5.5s]" + " %(message)s - [%(pathname)s.%(funcName)s:%(lineno)d]" +) + +DEFAULT_DATE_FORMAT = "%m/%d/%Y %I:%M:%S %p" + # an example of what should be in the ~/.aprsd/config.yml DEFAULT_CONFIG_DICT = { "ham": {"callsign": "CALLSIGN"}, @@ -22,6 +29,8 @@ DEFAULT_CONFIG_DICT = { }, "aprsd": { "logfile": "/tmp/aprsd.log", + "logformat": DEFAULT_LOG_FORMAT, + "dateformat": DEFAULT_DATE_FORMAT, "trace": False, "plugin_dir": "~/.config/aprsd/plugins", "enabled_plugins": plugin.CORE_PLUGINS,