From 2d64ee5e2a2b33aedf5e6d4bee7eaf8d250599fa Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Sat, 23 May 2020 00:08:00 -0400 Subject: [PATCH 1/5] Base extension logger level on the level of its parent. --- allianceauth/services/hooks.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/allianceauth/services/hooks.py b/allianceauth/services/hooks.py index 10a30fa3..0158d2d5 100644 --- a/allianceauth/services/hooks.py +++ b/allianceauth/services/hooks.py @@ -21,13 +21,12 @@ def get_extension_logger(name): :return: an extensions child logger """ import logging - from django.conf import settings + + parent_logger = logging.getLogger('extensions') logger = logging.getLogger('extensions.' + name) logger.name = name - logger.level = logging.INFO - if settings.DEBUG: - logger.level = logging.DEBUG + logger.level = parent_logger.level return logger From 6c275d4cd2cbcf9a568ca933268b9f626195ef36 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Sat, 23 May 2020 00:08:37 -0400 Subject: [PATCH 2/5] Add comment to local describing how to change logger level. Also update docs to be consistent with this change. --- .../project_name/settings/local.py | 4 ++++ docs/development/custom/logging.md | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/allianceauth/project_template/project_name/settings/local.py b/allianceauth/project_template/project_name/settings/local.py index 786bf38c..31f27fc1 100644 --- a/allianceauth/project_template/project_name/settings/local.py +++ b/allianceauth/project_template/project_name/settings/local.py @@ -22,6 +22,10 @@ INSTALLED_APPS += [ ] +# To change the logging level for extensions, uncomment the following line. +# LOGGING['handlers']['extension_file']['level'] = 'INFO' + + # Enter credentials to use MySQL/MariaDB. Comment out to use sqlite3 DATABASES['default'] = { 'ENGINE': 'django.db.backends.mysql', diff --git a/docs/development/custom/logging.md b/docs/development/custom/logging.md index edba7315..02c253eb 100644 --- a/docs/development/custom/logging.md +++ b/docs/development/custom/logging.md @@ -12,4 +12,22 @@ logger = get_extension_logger(__name__) ``` This works by creating a child logger of the extension logger which propagates all log entries -to the parent (extensions) logger. \ No newline at end of file +to the parent (extensions) logger. + +## Changing the Logging Level +By default, the extension logger's level is set to `DEBUG`. +To change this, uncomment (or add) the following line in `local.py`. + +```python +LOGGING['handlers']['extension_file']['level'] = 'INFO' +``` +*(Remember to restart your supervisor workers after changes to `local.py`)* + +This will change the logger's level to the level you define. + +Options are: *(all options accept entries of levels listed below them)* +* `DEBUG` +* `INFO` +* `WARNING` +* `ERROR` +* `CRITICAL` From 530716d4581b419de359cc89488c2c741867d4f9 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Tue, 26 May 2020 13:25:48 -0400 Subject: [PATCH 3/5] Fix docstring n `get_extension_logger` --- allianceauth/services/hooks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/allianceauth/services/hooks.py b/allianceauth/services/hooks.py index 0158d2d5..72fd8d0e 100644 --- a/allianceauth/services/hooks.py +++ b/allianceauth/services/hooks.py @@ -14,8 +14,7 @@ def get_extension_logger(name): Takes the name of a plugin/extension and generates a child logger of the extensions logger to be used by the extension to log events to the extensions logger. - The logging level is decided by whether or not DEBUG is set to true in the project settings. If - DEBUG is set to false, then the logging level is set to INFO. + The logging level is determined by the level defined for the parent logger. :param: name: the name of the extension doing the logging :return: an extensions child logger From db51abec1fa13805c32d1ed27eeb69fe4c696d92 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Tue, 26 May 2020 13:26:19 -0400 Subject: [PATCH 4/5] Change default logging level for extension logger. --- allianceauth/project_template/project_name/settings/base.py | 2 +- allianceauth/project_template/project_name/settings/local.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/allianceauth/project_template/project_name/settings/base.py b/allianceauth/project_template/project_name/settings/base.py index 72c1ddb8..c9a45b6f 100644 --- a/allianceauth/project_template/project_name/settings/base.py +++ b/allianceauth/project_template/project_name/settings/base.py @@ -221,7 +221,7 @@ LOGGING = { 'backupCount': 5, # edit this line to change number of log backups }, 'extension_file': { - 'level': 'DEBUG', + 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(BASE_DIR, 'log/extensions.log'), 'formatter': 'verbose', diff --git a/allianceauth/project_template/project_name/settings/local.py b/allianceauth/project_template/project_name/settings/local.py index 31f27fc1..8761facb 100644 --- a/allianceauth/project_template/project_name/settings/local.py +++ b/allianceauth/project_template/project_name/settings/local.py @@ -23,7 +23,7 @@ INSTALLED_APPS += [ ] # To change the logging level for extensions, uncomment the following line. -# LOGGING['handlers']['extension_file']['level'] = 'INFO' +# LOGGING['handlers']['extension_file']['level'] = 'DEBUG' # Enter credentials to use MySQL/MariaDB. Comment out to use sqlite3 From 250cb332855724b023a84d11f3a0fdf8d0da7b63 Mon Sep 17 00:00:00 2001 From: Col Crunch Date: Tue, 26 May 2020 13:49:06 -0400 Subject: [PATCH 5/5] Raise an error if `get_extension_logger` recieves a non-string argument. --- allianceauth/services/hooks.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/allianceauth/services/hooks.py b/allianceauth/services/hooks.py index 72fd8d0e..455319f3 100644 --- a/allianceauth/services/hooks.py +++ b/allianceauth/services/hooks.py @@ -19,6 +19,10 @@ def get_extension_logger(name): :param: name: the name of the extension doing the logging :return: an extensions child logger """ + if not isinstance(name, str): + raise TypeError(f"get_extension_logger takes an argument of type string." + f"Instead received argument of type {type(name).__name__}.") + import logging parent_logger = logging.getLogger('extensions')