From 4edfb9b94d2cc5433611b335a1e38e9c6f23e506 Mon Sep 17 00:00:00 2001 From: Deborah Kaplan Date: Wed, 27 Nov 2024 07:03:14 -0800 Subject: [PATCH] feat: allowing the passing of a logging formatter (#2644) * feat: allowing the passing of a logging formatter * allow the configuration of logging to pass a custom format string for the logs * adding type hints and a more complete docstring to the modified method * Making it we can modify config values via yaml (ie. without passing a python function) FIXES: APER-3805 * chore: Apply suggestions from code review the style guide calls for this exception to pep 257. Co-authored-by: Jason Wesson --------- Co-authored-by: Jason Wesson --- credentials/settings/production.py | 3 ++- credentials/settings/utils.py | 38 ++++++++++++++++++++---------- credentials/tests/test_utils.py | 10 ++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/credentials/settings/production.py b/credentials/settings/production.py index 74c644a56..43c3e92cd 100644 --- a/credentials/settings/production.py +++ b/credentials/settings/production.py @@ -12,7 +12,8 @@ ALLOWED_HOSTS = ["*"] -LOGGING = get_logger_config() +LOGGING_FORMAT_STRING = "" +LOGGING = get_logger_config(format_string=LOGGING_FORMAT_STRING) # Keep track of the names of settings that represent dicts. Instead of overriding the values in base.py, # the values read from disk should UPDATE the pre-configured dicts. diff --git a/credentials/settings/utils.py b/credentials/settings/utils.py index 07380dddc..7fc6e259e 100644 --- a/credentials/settings/utils.py +++ b/credentials/settings/utils.py @@ -16,22 +16,30 @@ def get_env_setting(setting): def get_logger_config( - log_dir="/var/tmp", - logging_env="no_env", - edx_filename="edx.log", - dev_env=False, - debug=False, - local_loglevel="INFO", - service_variant="credentials", + log_dir: str = "/var/tmp", + logging_env: str = "no_env", + edx_filename: str = "edx.log", + dev_env: bool = False, + debug: bool = False, + local_loglevel: str = "INFO", + service_variant: str = "credentials", + format_string: str = "", ): """ - Return the appropriate logging config dictionary. You should assign the - result of this to the LOGGING var in your settings. + Return the appropriate logging config dictionary, to be assigned to the LOGGING var in settings. - If dev_env is set to true logging will not be done via local rsyslogd, - instead, application logs will be dropped in log_dir. + Arguments: + log_dir (str): Location for log files + logging_env (str): + edx_filename (str): Filename base for logfiles when dev_env is enabled. + dev_env (bool): If False logging will use local rsyslogd, if True, application logs will go to log_dir. + debug (bool): Debug logging enabled. + local_loglevel (str): + service_variant (str): Name of the service. + format_string (str): Format string for your logfiles. - "edx_filename" is ignored unless dev_env is set to true since otherwise logging is handled by rsyslogd. + Returns: + dict(string): Returns a dictionary of config values """ # Revert to INFO if an invalid string is passed in @@ -48,12 +56,16 @@ def get_logger_config( handlers = ["console"] + standard_format = ( + format_string or "%(asctime)s %(levelname)s %(process)d [%(name)s] %(filename)s:%(lineno)d - %(message)s" + ) + logger_config = { "version": 1, "disable_existing_loggers": False, "formatters": { "standard": { - "format": "%(asctime)s %(levelname)s %(process)d " "[%(name)s] %(filename)s:%(lineno)d - %(message)s", + "format": standard_format, }, "syslog_format": {"format": syslog_format}, "raw": {"format": "%(message)s"}, diff --git a/credentials/tests/test_utils.py b/credentials/tests/test_utils.py index 7d803cd7e..a5655637c 100644 --- a/credentials/tests/test_utils.py +++ b/credentials/tests/test_utils.py @@ -103,6 +103,16 @@ def test_logging_env(self): config = get_logger_config() self.assertIn("env:no_env", config["formatters"]["syslog_format"]["format"]) + def test_format_string(self): + expected_default = "%(asctime)s %(levelname)s %(process)d [%(name)s] %(filename)s:%(lineno)d - %(message)s" + expected_configured = "%(message)s for everyone" + + config = get_logger_config() + self.assertIn(expected_default, config["formatters"]["standard_format"]["format"]) + + config = get_logger_config(format_string=expected_configured) + self.assertIn(expected_configured, config["formatters"]["syslog_format"]["format"]) + def test_edx_filename(self): config = get_logger_config(dev_env=True) self.assertIn("/var/tmp/edx.log", config["handlers"]["local"]["filename"])