Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Auto set logging filter #8051

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog.d/8051.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It is no longer necessary to explicitly define `filters` in the logging configuration. (Continuing to do so is redundant but harmless.)
6 changes: 0 additions & 6 deletions docker/conf/log.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ formatters:
precise:
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'

filters:
context:
(): synapse.logging.context.LoggingContextFilter
request: ""

handlers:
console:
class: logging.StreamHandler
formatter: precise
filters: [context]

loggers:
synapse.storage.SQL:
Expand Down
7 changes: 0 additions & 7 deletions docs/sample_log_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ formatters:
precise:
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'

filters:
context:
(): synapse.logging.context.LoggingContextFilter
request: ""

handlers:
file:
class: logging.handlers.TimedRotatingFileHandler
Expand All @@ -30,7 +25,6 @@ handlers:
# logs will still be flushed immediately.
buffer:
class: logging.handlers.MemoryHandler
filters: [context]
target: file
# The capacity is the number of log lines that are buffered before
# being written to disk. Increasing this will lead to better
Expand All @@ -44,7 +38,6 @@ handlers:
console:
class: logging.StreamHandler
formatter: precise
filters: [context]

loggers:
synapse.storage.SQL:
Expand Down
24 changes: 16 additions & 8 deletions synapse/config/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - \
%(request)s - %(message)s'

filters:
context:
(): synapse.logging.context.LoggingContextFilter
request: ""

handlers:
file:
class: logging.handlers.TimedRotatingFileHandler
Expand All @@ -74,7 +69,6 @@
# logs will still be flushed immediately.
buffer:
class: logging.handlers.MemoryHandler
filters: [context]
target: file
# The capacity is the number of log lines that are buffered before
# being written to disk. Increasing this will lead to better
Expand All @@ -88,7 +82,6 @@
console:
class: logging.StreamHandler
formatter: precise
filters: [context]

loggers:
synapse.storage.SQL:
Expand Down Expand Up @@ -199,11 +192,26 @@ def _setup_stdlib_logging(config, log_config, logBeginner: LogBeginner):

handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.addFilter(LoggingContextFilter(request=""))
logger.addHandler(handler)
else:
logging.config.dictConfig(log_config)

# We add a log record factory that runs all messages through the
# LoggingContextFilter so that we get the context *at the time we log*
# rather than when we write to a handler. This can be done in config using
# filter options, but care must when using e.g. MemoryHandler to buffer
# writes.

log_filter = LoggingContextFilter(request="")
old_factory = logging.getLogRecordFactory()

def factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
log_filter.filter(record)
return record

logging.setLogRecordFactory(factory)

# Route Twisted's native logging through to the standard library logging
# system.
observer = STDLibLogObserver()
Expand Down