diff --git a/allianceauth/services/hooks.py b/allianceauth/services/hooks.py index 44c90900..10a30fa3 100644 --- a/allianceauth/services/hooks.py +++ b/allianceauth/services/hooks.py @@ -9,6 +9,29 @@ from allianceauth.hooks import get_hooks from .models import NameFormatConfig +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. + + :param: name: the name of the extension doing the logging + :return: an extensions child logger + """ + import logging + from django.conf import settings + + logger = logging.getLogger('extensions.' + name) + logger.name = name + logger.level = logging.INFO + if settings.DEBUG: + logger.level = logging.DEBUG + + return logger + + class ServicesHook: """ Abstract base class for creating a compatible services diff --git a/docs/development/custom/logging.md b/docs/development/custom/logging.md index 9727e339..edba7315 100644 --- a/docs/development/custom/logging.md +++ b/docs/development/custom/logging.md @@ -2,15 +2,13 @@ Alliance Auth provides a logger for use with custom apps to make everyone's life a little easier. ## Using the Extensions Logger -The extensions logger should not be directly used by custom apps as the error messages logged to it -will not be labeled with the correct name. In order to correctly use the extensions logger please follow -the code below. +AllianceAuth provides a helper function to get the logger for the current module to reduce the amount of +code you need to write. ```python -import logging +from allianceauth.services.hooks import get_extension_logger -logger = logging.getLogger('extensions.' + __name__) -logger.name = __name__ +logger = get_extension_logger(__name__) ``` This works by creating a child logger of the extension logger which propagates all log entries