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

Change the default log config to reduce disk I/O and storage #8040

Merged
merged 11 commits into from
Aug 11, 2020
1 change: 1 addition & 0 deletions changelog.d/8040.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the default log config to reduce disk I/O and storage for new servers.
30 changes: 19 additions & 11 deletions docs/sample_log_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,41 @@ 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.RotatingFileHandler
class: logging.handlers.TimedRotatingFileHandler
formatter: precise
filename: /var/log/matrix-synapse/homeserver.log
maxBytes: 104857600
backupCount: 10
filters: [context]
when: midnight
backupCount: 3 # Does not include the current log file.
encoding: utf8

# Default to buffering writes to log file for efficiency. This means that
# will be a delay for WARNING/INFO/DEBUG logs to get written, but ERROR
# logs will still be flushed immediately.
buffer:
richvdh marked this conversation as resolved.
Show resolved Hide resolved
class: logging.handlers.MemoryHandler
target: file
capacity: 100
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of wonder if 100 is a bit extreme here for a new server?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as in, too high? maybe a bit.

probably worth adding a comment here saying what the number means, so people can tweak it if they want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, even jki.re logs at less than 30 lines a second so a brand new server would probably not actually flush very often at all, which I can imagine will cause confusion. I'm somewhat tempted to lower it to something like ten, which would still be a huge improvement.


console:
richvdh marked this conversation as resolved.
Show resolved Hide resolved
class: logging.StreamHandler
formatter: precise
filters: [context]

loggers:
synapse.storage.SQL:
# beware: increasing this to DEBUG will make synapse log sensitive
# information such as access tokens.
level: INFO

twisted:
# We send the twisted logging directly to the file handler,
# to work around https://github.com/matrix-org/synapse/issues/3471
handlers: [file]
propagate: false

root:
level: INFO
handlers: [file, console]
handlers: [buffer]

disable_existing_loggers: false
47 changes: 35 additions & 12 deletions synapse/config/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,42 @@
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw: remember that the docker image has its own default log config; please update it to keep it in sync.

%(request)s - %(message)s'

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

handlers:
file:
class: logging.handlers.RotatingFileHandler
class: logging.handlers.TimedRotatingFileHandler
formatter: precise
filename: ${log_file}
maxBytes: 104857600
backupCount: 10
filters: [context]
when: midnight
backupCount: 3 # Does not include the current log file.
encoding: utf8

# Default to buffering writes to log file for efficiency. This means that
# will be a delay for WARNING/INFO/DEBUG logs to get written, but ERROR
# logs will still be flushed immediately.
buffer:
class: logging.handlers.MemoryHandler
target: file
capacity: 100

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

loggers:
synapse.storage.SQL:
# beware: increasing this to DEBUG will make synapse log sensitive
# information such as access tokens.
level: INFO

twisted:
# We send the twisted logging directly to the file handler,
# to work around https://github.com/matrix-org/synapse/issues/3471
handlers: [file]
propagate: false

root:
level: INFO
handlers: [file, console]
handlers: [buffer]

disable_existing_loggers: false
"""
Expand Down Expand Up @@ -168,11 +176,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