2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2021-12-01 07:28:23 -05:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2014-08-31 11:06:39 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-01-03 12:11:29 -05:00
|
|
|
import argparse
|
2014-08-31 11:06:39 -04:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2015-04-30 11:52:57 -04:00
|
|
|
import os
|
2018-06-20 10:33:14 -04:00
|
|
|
import sys
|
2020-09-07 11:54:30 -04:00
|
|
|
import threading
|
2018-07-09 02:09:20 -04:00
|
|
|
from string import Template
|
2021-12-01 07:28:23 -05:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict, Optional
|
2015-04-30 11:52:57 -04:00
|
|
|
|
2018-06-20 10:33:14 -04:00
|
|
|
import yaml
|
2021-03-09 07:41:32 -05:00
|
|
|
from zope.interface import implementer
|
2018-06-20 10:33:14 -04:00
|
|
|
|
2019-09-12 12:29:55 -04:00
|
|
|
from twisted.logger import (
|
2021-03-09 07:41:32 -05:00
|
|
|
ILogObserver,
|
2019-09-12 12:29:55 -04:00
|
|
|
LogBeginner,
|
|
|
|
STDLibLogObserver,
|
2020-09-07 11:54:30 -04:00
|
|
|
eventAsText,
|
2019-09-12 12:29:55 -04:00
|
|
|
globalLogBeginner,
|
|
|
|
)
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import LoggingContextFilter
|
2020-10-29 07:27:37 -04:00
|
|
|
from synapse.logging.filter import MetadataFilter
|
2022-04-11 12:07:23 -04:00
|
|
|
from synapse.types import JsonDict
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2022-06-07 10:24:11 -04:00
|
|
|
from ..util import SYNAPSE_VERSION
|
2020-01-03 12:11:29 -05:00
|
|
|
from ._base import Config, ConfigError
|
2015-04-30 11:52:57 -04:00
|
|
|
|
2021-10-22 13:15:41 -04:00
|
|
|
if TYPE_CHECKING:
|
2021-12-01 07:28:23 -05:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2021-10-22 13:15:41 -04:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
DEFAULT_LOG_CONFIG = Template(
|
2020-01-03 12:14:00 -05:00
|
|
|
"""\
|
|
|
|
# Log configuration for Synapse.
|
|
|
|
#
|
|
|
|
# This is a YAML file containing a standard Python logging configuration
|
|
|
|
# dictionary. See [1] for details on the valid settings.
|
|
|
|
#
|
2020-10-29 07:27:37 -04:00
|
|
|
# Synapse also supports structured logging for machine readable logs which can
|
|
|
|
# be ingested by ELK stacks. See [2] for details.
|
|
|
|
#
|
2020-01-03 12:14:00 -05:00
|
|
|
# [1]: https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
|
2021-07-07 07:35:45 -04:00
|
|
|
# [2]: https://matrix-org.github.io/synapse/latest/structured_logging.html
|
2020-01-03 12:14:00 -05:00
|
|
|
|
2015-04-30 11:52:57 -04:00
|
|
|
version: 1
|
|
|
|
|
|
|
|
formatters:
|
2018-01-09 06:27:19 -05:00
|
|
|
precise:
|
|
|
|
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - \
|
|
|
|
%(request)s - %(message)s'
|
2015-04-30 11:52:57 -04:00
|
|
|
|
|
|
|
handlers:
|
2018-01-09 06:27:19 -05:00
|
|
|
file:
|
2020-08-11 13:09:46 -04:00
|
|
|
class: logging.handlers.TimedRotatingFileHandler
|
2018-01-09 06:27:19 -05:00
|
|
|
formatter: precise
|
|
|
|
filename: ${log_file}
|
2020-08-11 13:09:46 -04:00
|
|
|
when: midnight
|
|
|
|
backupCount: 3 # Does not include the current log file.
|
2018-11-02 11:28:07 -04:00
|
|
|
encoding: utf8
|
2020-08-11 13:09:46 -04:00
|
|
|
|
2021-08-17 08:13:11 -04:00
|
|
|
# Default to buffering writes to log file for efficiency.
|
|
|
|
# WARNING/ERROR logs will still be flushed immediately, but there will be a
|
|
|
|
# delay (of up to `period` seconds, or until the buffer is full with
|
|
|
|
# `capacity` messages) before INFO/DEBUG logs get written.
|
2020-08-11 13:09:46 -04:00
|
|
|
buffer:
|
2021-08-17 08:13:11 -04:00
|
|
|
class: synapse.logging.handlers.PeriodicallyFlushingMemoryHandler
|
2020-08-11 13:09:46 -04:00
|
|
|
target: file
|
2021-08-17 08:13:11 -04:00
|
|
|
|
|
|
|
# The capacity is the maximum number of log lines that are buffered
|
|
|
|
# before being written to disk. Increasing this will lead to better
|
2020-08-11 13:09:46 -04:00
|
|
|
# performance, at the expensive of it taking longer for log lines to
|
|
|
|
# be written to disk.
|
2021-08-17 08:13:11 -04:00
|
|
|
# This parameter is required.
|
2020-08-11 13:09:46 -04:00
|
|
|
capacity: 10
|
2021-08-17 08:13:11 -04:00
|
|
|
|
|
|
|
# Logs with a level at or above the flush level will cause the buffer to
|
|
|
|
# be flushed immediately.
|
|
|
|
# Default value: 40 (ERROR)
|
|
|
|
# Other values: 50 (CRITICAL), 30 (WARNING), 20 (INFO), 10 (DEBUG)
|
|
|
|
flushLevel: 30 # Flush immediately for WARNING logs and higher
|
|
|
|
|
|
|
|
# The period of time, in seconds, between forced flushes.
|
|
|
|
# Messages will not be delayed for longer than this time.
|
|
|
|
# Default value: 5 seconds
|
|
|
|
period: 5
|
2020-08-11 13:09:46 -04:00
|
|
|
|
|
|
|
# A handler that writes logs to stderr. Unused by default, but can be used
|
|
|
|
# instead of "buffer" and "file" in the logger handlers.
|
2018-01-09 06:27:19 -05:00
|
|
|
console:
|
|
|
|
class: logging.StreamHandler
|
|
|
|
formatter: precise
|
2015-04-30 11:52:57 -04:00
|
|
|
|
|
|
|
loggers:
|
|
|
|
synapse.storage.SQL:
|
2017-03-10 10:23:20 -05:00
|
|
|
# beware: increasing this to DEBUG will make synapse log sensitive
|
|
|
|
# information such as access tokens.
|
2015-04-30 11:52:57 -04:00
|
|
|
level: INFO
|
|
|
|
|
|
|
|
root:
|
|
|
|
level: INFO
|
2020-08-11 13:09:46 -04:00
|
|
|
|
|
|
|
# Write logs to the `buffer` handler, which will buffer them together in memory,
|
|
|
|
# then write them to a file.
|
|
|
|
#
|
|
|
|
# Replace "buffer" with "console" to log to stderr instead. (Note that you'll
|
2020-10-23 12:38:40 -04:00
|
|
|
# also need to update the configuration for the `twisted` logger above, in
|
2020-08-11 13:09:46 -04:00
|
|
|
# this case.)
|
|
|
|
#
|
|
|
|
handlers: [buffer]
|
2019-10-18 07:34:33 -04:00
|
|
|
|
|
|
|
disable_existing_loggers: false
|
2019-06-20 05:32:02 -04:00
|
|
|
"""
|
|
|
|
)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2020-01-03 12:11:29 -05:00
|
|
|
LOG_FILE_ERROR = """\
|
|
|
|
Support for the log_file configuration option and --log-file command-line option was
|
|
|
|
removed in Synapse 1.3.0. You should instead set up a separate log configuration file.
|
|
|
|
"""
|
|
|
|
|
2022-02-17 08:32:18 -05:00
|
|
|
STRUCTURED_ERROR = """\
|
|
|
|
Support for the structured configuration option was removed in Synapse 1.54.0.
|
|
|
|
You should instead use the standard logging configuration. See
|
|
|
|
https://matrix-org.github.io/synapse/v1.54/structured_logging.html
|
|
|
|
"""
|
|
|
|
|
2014-10-30 06:13:46 -04:00
|
|
|
|
2014-08-31 11:06:39 -04:00
|
|
|
class LoggingConfig(Config):
|
2019-10-10 04:39:35 -04:00
|
|
|
section = "logging"
|
|
|
|
|
2022-04-11 12:07:23 -04:00
|
|
|
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
2020-01-03 12:11:29 -05:00
|
|
|
if config.get("log_file"):
|
|
|
|
raise ConfigError(LOG_FILE_ERROR)
|
2015-04-29 23:24:44 -04:00
|
|
|
self.log_config = self.abspath(config.get("log_config"))
|
2019-07-18 11:40:08 -04:00
|
|
|
self.no_redirect_stdio = config.get("no_redirect_stdio", False)
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2022-04-11 12:07:23 -04:00
|
|
|
def generate_config_section(
|
|
|
|
self, config_dir_path: str, server_name: str, **kwargs: Any
|
|
|
|
) -> str:
|
2018-12-21 10:04:57 -05:00
|
|
|
log_config = os.path.join(config_dir_path, server_name + ".log.config")
|
2019-06-20 05:32:02 -04:00
|
|
|
return (
|
|
|
|
"""\
|
2015-04-30 11:52:57 -04:00
|
|
|
log_config: "%(log_config)s"
|
2019-06-20 05:32:02 -04:00
|
|
|
"""
|
|
|
|
% locals()
|
|
|
|
)
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
def read_arguments(self, args: argparse.Namespace) -> None:
|
2017-03-10 10:38:29 -05:00
|
|
|
if args.no_redirect_stdio is not None:
|
|
|
|
self.no_redirect_stdio = args.no_redirect_stdio
|
2020-01-03 12:11:29 -05:00
|
|
|
if args.log_file is not None:
|
|
|
|
raise ConfigError(LOG_FILE_ERROR)
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2019-07-15 08:15:34 -04:00
|
|
|
@staticmethod
|
2021-12-01 07:28:23 -05:00
|
|
|
def add_arguments(parser: argparse.ArgumentParser) -> None:
|
2014-08-31 11:06:39 -04:00
|
|
|
logging_group = parser.add_argument_group("logging")
|
2017-03-10 10:38:29 -05:00
|
|
|
logging_group.add_argument(
|
2019-06-20 05:32:02 -04:00
|
|
|
"-n",
|
|
|
|
"--no-redirect-stdio",
|
|
|
|
action="store_true",
|
|
|
|
default=None,
|
|
|
|
help="Do not redirect stdout/stderr to the log",
|
2017-03-10 10:38:29 -05:00
|
|
|
)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2020-01-03 12:11:29 -05:00
|
|
|
logging_group.add_argument(
|
2021-02-16 17:32:34 -05:00
|
|
|
"-f",
|
|
|
|
"--log-file",
|
|
|
|
dest="log_file",
|
|
|
|
help=argparse.SUPPRESS,
|
2020-01-03 12:11:29 -05:00
|
|
|
)
|
|
|
|
|
2021-11-23 10:21:19 -05:00
|
|
|
def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
|
2015-04-30 11:52:57 -04:00
|
|
|
log_config = config.get("log_config")
|
|
|
|
if log_config and not os.path.exists(log_config):
|
2018-01-09 06:28:33 -05:00
|
|
|
log_file = self.abspath("homeserver.log")
|
2019-06-21 12:14:56 -04:00
|
|
|
print(
|
|
|
|
"Generating log config file %s which will log to %s"
|
|
|
|
% (log_config, log_file)
|
|
|
|
)
|
2018-04-28 06:29:02 -04:00
|
|
|
with open(log_config, "w") as log_config_file:
|
2019-06-20 05:32:02 -04:00
|
|
|
log_config_file.write(DEFAULT_LOG_CONFIG.substitute(log_file=log_file))
|
2015-04-30 11:52:57 -04:00
|
|
|
|
2016-06-16 06:06:12 -04:00
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
def _setup_stdlib_logging(
|
|
|
|
config: "HomeServerConfig", log_config_path: Optional[str], logBeginner: LogBeginner
|
|
|
|
) -> None:
|
2019-08-28 07:18:53 -04:00
|
|
|
"""
|
2020-10-29 07:27:37 -04:00
|
|
|
Set up Python standard library logging.
|
2017-03-10 10:16:50 -05:00
|
|
|
"""
|
2020-10-29 07:27:37 -04:00
|
|
|
if log_config_path is None:
|
2019-07-18 11:40:08 -04:00
|
|
|
log_format = (
|
|
|
|
"%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s"
|
|
|
|
" - %(message)s"
|
|
|
|
)
|
2016-06-16 06:06:12 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
logger = logging.getLogger("")
|
2019-07-18 11:40:08 -04:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
logging.getLogger("synapse.storage.SQL").setLevel(logging.INFO)
|
2016-06-16 06:06:12 -04:00
|
|
|
|
|
|
|
formatter = logging.Formatter(log_format)
|
2017-10-26 11:45:20 -04:00
|
|
|
|
2019-07-18 11:40:08 -04:00
|
|
|
handler = logging.StreamHandler()
|
2016-06-16 06:06:12 -04:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
else:
|
2020-10-29 07:27:37 -04:00
|
|
|
# Load the logging configuration.
|
|
|
|
_load_logging_config(log_config_path)
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-08-07 06:39:29 -04:00
|
|
|
# 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.
|
|
|
|
|
2020-12-14 14:19:47 -05:00
|
|
|
log_context_filter = LoggingContextFilter()
|
2021-09-13 13:07:12 -04:00
|
|
|
log_metadata_filter = MetadataFilter({"server_name": config.server.server_name})
|
2020-08-07 06:39:29 -04:00
|
|
|
old_factory = logging.getLogRecordFactory()
|
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
def factory(*args: Any, **kwargs: Any) -> logging.LogRecord:
|
2020-08-07 06:39:29 -04:00
|
|
|
record = old_factory(*args, **kwargs)
|
2020-10-29 07:27:37 -04:00
|
|
|
log_context_filter.filter(record)
|
|
|
|
log_metadata_filter.filter(record)
|
2020-08-07 06:39:29 -04:00
|
|
|
return record
|
|
|
|
|
|
|
|
logging.setLogRecordFactory(factory)
|
|
|
|
|
2019-08-28 07:18:53 -04:00
|
|
|
# Route Twisted's native logging through to the standard library logging
|
|
|
|
# system.
|
2016-12-30 06:05:37 -05:00
|
|
|
observer = STDLibLogObserver()
|
2018-09-13 05:59:32 -04:00
|
|
|
|
2020-09-07 11:54:30 -04:00
|
|
|
threadlocal = threading.local()
|
2018-09-13 05:59:32 -04:00
|
|
|
|
2021-03-09 07:41:32 -05:00
|
|
|
@implementer(ILogObserver)
|
|
|
|
def _log(event: dict) -> None:
|
2018-09-13 05:59:32 -04:00
|
|
|
if "log_text" in event:
|
|
|
|
if event["log_text"].startswith("DNSDatagramProtocol starting on "):
|
|
|
|
return
|
|
|
|
|
|
|
|
if event["log_text"].startswith("(UDP Port "):
|
|
|
|
return
|
|
|
|
|
|
|
|
if event["log_text"].startswith("Timing out client"):
|
|
|
|
return
|
|
|
|
|
2020-09-07 11:54:30 -04:00
|
|
|
# this is a workaround to make sure we don't get stack overflows when the
|
|
|
|
# logging system raises an error which is written to stderr which is redirected
|
|
|
|
# to the logging system, etc.
|
|
|
|
if getattr(threadlocal, "active", False):
|
|
|
|
# write the text of the event, if any, to the *real* stderr (which may
|
|
|
|
# be redirected to /dev/null, but there's not much we can do)
|
|
|
|
try:
|
|
|
|
event_text = eventAsText(event)
|
|
|
|
print("logging during logging: %s" % event_text, file=sys.__stderr__)
|
|
|
|
except Exception:
|
|
|
|
# gah.
|
|
|
|
pass
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
threadlocal.active = True
|
|
|
|
return observer(event)
|
|
|
|
finally:
|
|
|
|
threadlocal.active = False
|
2018-09-13 05:59:32 -04:00
|
|
|
|
2021-06-21 06:41:25 -04:00
|
|
|
logBeginner.beginLoggingTo([_log], redirectStandardIO=False)
|
2019-08-28 07:18:53 -04:00
|
|
|
|
|
|
|
|
2020-10-29 07:27:37 -04:00
|
|
|
def _load_logging_config(log_config_path: str) -> None:
|
|
|
|
"""
|
|
|
|
Configure logging from a log config path.
|
|
|
|
"""
|
|
|
|
with open(log_config_path, "rb") as f:
|
|
|
|
log_config = yaml.safe_load(f.read())
|
2019-08-28 07:18:53 -04:00
|
|
|
|
|
|
|
if not log_config:
|
2020-10-29 07:27:37 -04:00
|
|
|
logging.warning("Loaded a blank logging config?")
|
|
|
|
|
2022-02-17 08:32:18 -05:00
|
|
|
# If the old structured logging configuration is being used, raise an error.
|
2020-10-29 07:27:37 -04:00
|
|
|
if "structured" in log_config and log_config.get("structured"):
|
2022-02-17 08:32:18 -05:00
|
|
|
raise ConfigError(STRUCTURED_ERROR)
|
2019-08-28 07:18:53 -04:00
|
|
|
|
|
|
|
logging.config.dictConfig(log_config)
|
|
|
|
|
|
|
|
|
2021-12-01 07:28:23 -05:00
|
|
|
def _reload_logging_config(log_config_path: Optional[str]) -> None:
|
2020-10-29 07:27:37 -04:00
|
|
|
"""
|
|
|
|
Reload the log configuration from the file and apply it.
|
|
|
|
"""
|
|
|
|
# If no log config path was given, it cannot be reloaded.
|
|
|
|
if log_config_path is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
_load_logging_config(log_config_path)
|
|
|
|
logging.info("Reloaded log config from %s due to SIGHUP", log_config_path)
|
|
|
|
|
|
|
|
|
2019-09-12 12:29:55 -04:00
|
|
|
def setup_logging(
|
2021-10-22 13:15:41 -04:00
|
|
|
hs: "HomeServer",
|
2021-12-01 07:28:23 -05:00
|
|
|
config: "HomeServerConfig",
|
|
|
|
use_worker_options: bool = False,
|
2021-10-22 13:15:41 -04:00
|
|
|
logBeginner: LogBeginner = globalLogBeginner,
|
2020-10-29 07:27:37 -04:00
|
|
|
) -> None:
|
2019-08-28 07:18:53 -04:00
|
|
|
"""
|
|
|
|
Set up the logging subsystem.
|
|
|
|
|
|
|
|
Args:
|
2020-07-29 18:22:13 -04:00
|
|
|
config (LoggingConfig | synapse.config.worker.WorkerConfig):
|
2019-08-28 07:18:53 -04:00
|
|
|
configuration data
|
|
|
|
|
|
|
|
use_worker_options (bool): True to use the 'worker_log_config' option
|
|
|
|
instead of 'log_config'.
|
2019-09-12 12:29:55 -04:00
|
|
|
|
|
|
|
logBeginner: The Twisted logBeginner to use.
|
|
|
|
|
2019-08-28 07:18:53 -04:00
|
|
|
"""
|
2020-10-29 07:27:37 -04:00
|
|
|
log_config_path = (
|
2021-09-24 07:25:21 -04:00
|
|
|
config.worker.worker_log_config
|
|
|
|
if use_worker_options
|
|
|
|
else config.logging.log_config
|
2020-10-29 07:27:37 -04:00
|
|
|
)
|
2019-08-28 07:18:53 -04:00
|
|
|
|
2020-10-29 07:27:37 -04:00
|
|
|
# Perform one-time logging configuration.
|
|
|
|
_setup_stdlib_logging(config, log_config_path, logBeginner=logBeginner)
|
|
|
|
# Add a SIGHUP handler to reload the logging configuration, if one is available.
|
2021-04-23 14:20:44 -04:00
|
|
|
from synapse.app import _base as appbase
|
|
|
|
|
2020-10-29 07:27:37 -04:00
|
|
|
appbase.register_sighup(_reload_logging_config, log_config_path)
|
2019-08-28 07:18:53 -04:00
|
|
|
|
2020-10-29 07:27:37 -04:00
|
|
|
# Log immediately so we can grep backwards.
|
2019-11-01 08:03:44 -04:00
|
|
|
logging.warning("***** STARTING SERVER *****")
|
2022-02-14 08:12:22 -05:00
|
|
|
logging.warning(
|
|
|
|
"Server %s version %s",
|
|
|
|
sys.argv[0],
|
2022-06-07 10:24:11 -04:00
|
|
|
SYNAPSE_VERSION,
|
2022-02-14 08:12:22 -05:00
|
|
|
)
|
2021-09-13 13:07:12 -04:00
|
|
|
logging.info("Server hostname: %s", config.server.server_name)
|
2020-05-22 11:11:35 -04:00
|
|
|
logging.info("Instance name: %s", hs.get_instance_name())
|