logging: Set up a different logger for each backend.

Adds a top-level logger in `settings.LOGGING` `zulip.auth`
with the default handlers `DEFAULT_ZULIP_HANDLERS` and
an extra hanlder that writes to `/var/log/zulip/auth.log`.

Each auth backend uses it's own logger, `self.logger` which
is in form 'zulip.auth.<backend name>'.

This way it's clear which auth backend generated the log
and is easier to look for all authentication logs in one file.

Besides the above mentioned changes, `name` attribute is added to
`ZulipAuthMixin` so that these logging kind of calls wouldn't raise
any issues when logging is tried in a class without `name` attribute.

Also in the tests we use a new way to check if logger calls are made
i.e. we use `assertLogs` to test if something is logged.

Thanks to Mateusz Mandera for the idea of having a seperate logger
for auth backends and suggestion of using `assertLogs`.
This commit is contained in:
Dinesh
2020-06-02 23:50:53 +05:30
committed by Tim Abbott
parent b1b34b6f6a
commit d30f11888a
3 changed files with 127 additions and 99 deletions

View File

@@ -680,6 +680,7 @@ SOFT_DEACTIVATION_LOG_PATH = zulip_path("/var/log/zulip/soft_deactivation.log")
TRACEMALLOC_DUMP_DIR = zulip_path("/var/log/zulip/tracemalloc")
SCHEDULED_MESSAGE_DELIVERER_LOG_PATH = zulip_path("/var/log/zulip/scheduled_message_deliverer.log")
RETENTION_LOG_PATH = zulip_path("/var/log/zulip/message_retention.log")
AUTH_LOG_PATH = zulip_path("/var/log/zulip/auth.log")
# The EVENT_LOGS feature is an ultra-legacy piece of code, which
# originally logged all significant database changes for debugging.
@@ -753,6 +754,12 @@ LOGGING: Dict[str, Any] = {
if not DEBUG_ERROR_REPORTING else []),
'formatter': 'default'
},
'auth_file': {
'level': 'DEBUG',
'class': 'logging.handlers.WatchedFileHandler',
'formatter': 'default',
'filename': AUTH_LOG_PATH,
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
@@ -890,6 +897,10 @@ LOGGING: Dict[str, Any] = {
'zerver.management.commands.deliver_scheduled_messages': {
'level': 'DEBUG',
},
'zulip.auth': {
'level': 'DEBUG',
'handlers': DEFAULT_ZULIP_HANDLERS + ['auth_file']
},
'zulip.ldap': {
'level': 'DEBUG',
'handlers': ['console', 'ldap_file', 'errors_file'],