mirror of
https://github.com/zulip/zulip.git
synced 2025-11-17 04:12:02 +00:00
logging: Attribute each log message to the logger it came through.
This also gives us a place to hang the originating module, if we write a bit of logic to work that out; sadly it doesn't come out of the box, only the filename (which is likely to have a bunch of noise that just shows the path to the deployment or virtualenv.)
This commit is contained in:
@@ -125,11 +125,18 @@ def skip_site_packages_logs(record):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def find_log_origin(record):
|
||||||
|
# type: (logging.LogRecord) -> str
|
||||||
|
if record.name == 'root':
|
||||||
|
return ''
|
||||||
|
else:
|
||||||
|
return record.name
|
||||||
|
|
||||||
class ZulipFormatter(logging.Formatter):
|
class ZulipFormatter(logging.Formatter):
|
||||||
# Used in the base implementation. Default uses `,`.
|
# Used in the base implementation. Default uses `,`.
|
||||||
default_msec_format = '%s.%03d'
|
default_msec_format = '%s.%03d'
|
||||||
|
|
||||||
_fmt = '%(asctime)s %(levelname)-8s %(message)s'
|
_fmt = '%(asctime)s %(levelname)-8s [%(zulip_origin)s] %(message)s'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
@@ -137,6 +144,11 @@ class ZulipFormatter(logging.Formatter):
|
|||||||
|
|
||||||
def format(self, record):
|
def format(self, record):
|
||||||
# type: (logging.LogRecord) -> str
|
# type: (logging.LogRecord) -> str
|
||||||
|
if not getattr(record, 'zulip_decorated', False):
|
||||||
|
# The `setattr` calls put this logic explicitly outside the bounds of the
|
||||||
|
# type system; otherwise mypy would complain LogRecord lacks these attributes.
|
||||||
|
setattr(record, 'zulip_origin', find_log_origin(record))
|
||||||
|
setattr(record, 'zulip_decorated', True)
|
||||||
return super().format(record)
|
return super().format(record)
|
||||||
|
|
||||||
def create_logger(name, log_file, log_level, log_format="%(asctime)s %(levelname)-8s %(message)s"):
|
def create_logger(name, log_file, log_level, log_format="%(asctime)s %(levelname)-8s %(message)s"):
|
||||||
|
|||||||
Reference in New Issue
Block a user