Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allowing the passing of a logging formatter #2644

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 25 additions & 14 deletions credentials/settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@ 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 +55,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