Skip to content

Commit

Permalink
feat: allowing the passing of a logging formatter (#2644)
Browse files Browse the repository at this point in the history
* 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 <jsnwesson@gmail.com>

---------

Co-authored-by: Jason Wesson <jsnwesson@gmail.com>
  • Loading branch information
deborahgu and jsnwesson authored Nov 27, 2024
1 parent 98178a0 commit 4edfb9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
3 changes: 2 additions & 1 deletion credentials/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 25 additions & 13 deletions credentials/settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand Down
10 changes: 10 additions & 0 deletions credentials/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 4edfb9b

Please sign in to comment.