2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2021-12-01 05:28:23 -07:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2014-08-31 09:06:39 -06: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 10:11:29 -07:00
|
|
|
import argparse
|
2014-08-31 09:06:39 -06:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2015-04-30 09:52:57 -06:00
|
|
|
import os
|
2018-06-20 08:33:14 -06:00
|
|
|
import sys
|
2020-09-07 09:54:30 -06:00
|
|
|
import threading
|
2018-07-09 00:09:20 -06:00
|
|
|
from string import Template
|
2021-12-01 05:28:23 -07:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict, Optional
|
2015-04-30 09:52:57 -06:00
|
|
|
|
2018-06-20 08:33:14 -06:00
|
|
|
import yaml
|
2021-03-09 05:41:32 -07:00
|
|
|
from zope.interface import implementer
|
2018-06-20 08:33:14 -06:00
|
|
|
|
2019-09-12 10:29:55 -06:00
|
|
|
from twisted.logger import (
|
2021-03-09 05:41:32 -07:00
|
|
|
ILogObserver,
|
2019-09-12 10:29:55 -06:00
|
|
|
LogBeginner,
|
|
|
|
STDLibLogObserver,
|
2020-09-07 09:54:30 -06:00
|
|
|
eventAsText,
|
2019-09-12 10:29:55 -06:00
|
|
|
globalLogBeginner,
|
|
|
|
)
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2019-07-03 08:07:04 -06:00
|
|
|
from synapse.logging.context import LoggingContextFilter
|
2020-10-29 05:27:37 -06:00
|
|
|
from synapse.logging.filter import MetadataFilter
|
2023-02-03 06:27:31 -07:00
|
|
|
from synapse.synapse_rust import reset_logging_config
|
2022-04-11 10:07:23 -06:00
|
|
|
from synapse.types import JsonDict
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2022-06-07 08:24:11 -06:00
|
|
|
from ..util import SYNAPSE_VERSION
|
2020-01-03 10:11:29 -07:00
|
|
|
from ._base import Config, ConfigError
|
2015-04-30 09:52:57 -06:00
|
|
|
|
2021-10-22 11:15:41 -06:00
|
|
|
if TYPE_CHECKING:
|
2021-12-01 05:28:23 -07:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2021-10-22 11:15:41 -06:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2015-04-30 09:52:57 -06:00
|
|
|
DEFAULT_LOG_CONFIG = Template(
|
2020-01-03 10:14:00 -07: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 05:27:37 -06:00
|
|
|
# Synapse also supports structured logging for machine readable logs which can
|
|
|
|
# be ingested by ELK stacks. See [2] for details.
|
|
|
|
#
|
2022-10-27 07:39:47 -06:00
|
|
|
# [1]: https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema
|
2021-07-07 05:35:45 -06:00
|
|
|
# [2]: https://matrix-org.github.io/synapse/latest/structured_logging.html
|
2020-01-03 10:14:00 -07:00
|
|
|
|
2015-04-30 09:52:57 -06:00
|
|
|
version: 1
|
|
|
|
|
|
|
|
formatters:
|
2018-01-09 04:27:19 -07:00
|
|
|
precise:
|
|
|
|
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - \
|
|
|
|
%(request)s - %(message)s'
|
2015-04-30 09:52:57 -06:00
|
|
|
|
|
|
|
handlers:
|
2018-01-09 04:27:19 -07:00
|
|
|
file:
|
2020-08-11 11:09:46 -06:00
|
|
|
class: logging.handlers.TimedRotatingFileHandler
|
2018-01-09 04:27:19 -07:00
|
|
|
formatter: precise
|
|
|
|
filename: ${log_file}
|
2020-08-11 11:09:46 -06:00
|
|
|
when: midnight
|
|
|
|
backupCount: 3 # Does not include the current log file.
|
2018-11-02 09:28:07 -06:00
|
|
|
encoding: utf8
|
2020-08-11 11:09:46 -06:00
|
|
|
|
2021-08-17 06:13:11 -06: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 11:09:46 -06:00
|
|
|
buffer:
|
2021-08-17 06:13:11 -06:00
|
|
|
class: synapse.logging.handlers.PeriodicallyFlushingMemoryHandler
|
2020-08-11 11:09:46 -06:00
|
|
|
target: file
|
2021-08-17 06:13:11 -06: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 11:09:46 -06:00
|
|
|
# performance, at the expensive of it taking longer for log lines to
|
|
|
|
# be written to disk.
|
2021-08-17 06:13:11 -06:00
|
|
|
# This parameter is required.
|
2020-08-11 11:09:46 -06:00
|
|
|
capacity: 10
|
2021-08-17 06:13:11 -06: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 11:09:46 -06: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 04:27:19 -07:00
|
|
|
console:
|
|
|
|
class: logging.StreamHandler
|
|
|
|
formatter: precise
|
2015-04-30 09:52:57 -06:00
|
|
|
|
|
|
|
loggers:
|
|
|
|
synapse.storage.SQL:
|
2017-03-10 08:23:20 -07:00
|
|
|
# beware: increasing this to DEBUG will make synapse log sensitive
|
|
|
|
# information such as access tokens.
|
2015-04-30 09:52:57 -06:00
|
|
|
level: INFO
|
|
|
|
|
|
|
|
root:
|
|
|
|
level: INFO
|
2020-08-11 11:09:46 -06:00
|
|
|
|
|
|
|
# Write logs to the `buffer` handler, which will buffer them together in memory,
|
|
|
|
# then write them to a file.
|
|
|
|
#
|
2023-05-22 11:58:58 -06:00
|
|
|
# Replace "buffer" with "console" to log to stderr instead.
|
2020-08-11 11:09:46 -06:00
|
|
|
#
|
|
|
|
handlers: [buffer]
|
2019-10-18 05:34:33 -06:00
|
|
|
|
|
|
|
disable_existing_loggers: false
|
2015-04-30 09:52:57 -06:00
|
|
|
"""
|
|
|
|
)
|
2014-08-31 09:06:39 -06:00
|
|
|
|
2020-01-03 10:11:29 -07: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 06:32:18 -07: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 04:13:46 -06:00
|
|
|
|
2014-08-31 09:06:39 -06:00
|
|
|
class LoggingConfig(Config):
|
2019-10-10 02:39:35 -06:00
|
|
|
section = "logging"
|
|
|
|
|
2022-04-11 10:07:23 -06:00
|
|
|
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
2020-01-03 10:11:29 -07:00
|
|
|
if config.get("log_file"):
|
|
|
|
raise ConfigError(LOG_FILE_ERROR)
|
2015-04-29 21:24:44 -06:00
|
|
|
self.log_config = self.abspath(config.get("log_config"))
|
2019-07-18 09:40:08 -06:00
|
|
|
self.no_redirect_stdio = config.get("no_redirect_stdio", False)
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2022-04-11 10:07:23 -06:00
|
|
|
def generate_config_section(
|
|
|
|
self, config_dir_path: str, server_name: str, **kwargs: Any
|
|
|
|
) -> str:
|
2018-12-21 08:04:57 -07:00
|
|
|
log_config = os.path.join(config_dir_path, server_name + ".log.config")
|
2019-03-04 10:14:58 -07:00
|
|
|
return (
|
|
|
|
"""\
|
2015-04-30 09:52:57 -06:00
|
|
|
log_config: "%(log_config)s"
|
2015-04-29 21:24:44 -06:00
|
|
|
"""
|
|
|
|
% locals()
|
2019-06-20 03:32:02 -06:00
|
|
|
)
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2021-12-01 05:28:23 -07:00
|
|
|
def read_arguments(self, args: argparse.Namespace) -> None:
|
2017-03-10 08:38:29 -07:00
|
|
|
if args.no_redirect_stdio is not None:
|
|
|
|
self.no_redirect_stdio = args.no_redirect_stdio
|
2020-01-03 10:11:29 -07:00
|
|
|
if args.log_file is not None:
|
|
|
|
raise ConfigError(LOG_FILE_ERROR)
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2019-07-15 06:15:34 -06:00
|
|
|
@staticmethod
|
2021-12-01 05:28:23 -07:00
|
|
|
def add_arguments(parser: argparse.ArgumentParser) -> None:
|
2014-08-31 09:06:39 -06:00
|
|
|
logging_group = parser.add_argument_group("logging")
|
2017-03-10 08:38:29 -07:00
|
|
|
logging_group.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--no-redirect-stdio",
|
|
|
|
action="store_true",
|
|
|
|
default=None,
|
|
|
|
help="Do not redirect stdout/stderr to the log",
|
|
|
|
)
|
2014-08-31 09:06:39 -06:00
|
|
|
|
2020-01-03 10:11:29 -07:00
|
|
|
logging_group.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--log-file",
|
|
|
|
dest="log_file",
|
|
|
|
help=argparse.SUPPRESS,
|
|
|
|
)
|
|
|
|
|
2021-11-23 08:21:19 -07:00
|
|
|
def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
|
2015-04-30 09:52:57 -06:00
|
|
|
log_config = config.get("log_config")
|
|
|
|
if log_config and not os.path.exists(log_config):
|
2018-01-09 04:28:33 -07:00
|
|
|
log_file = self.abspath("homeserver.log")
|
2019-06-21 10:14:56 -06:00
|
|
|
print(
|
|
|
|
"Generating log config file %s which will log to %s"
|
|
|
|
% (log_config, log_file)
|
|
|
|
)
|
2018-04-28 04:29:02 -06:00
|
|
|
with open(log_config, "w") as log_config_file:
|
2018-01-09 04:28:33 -07:00
|
|
|
log_config_file.write(DEFAULT_LOG_CONFIG.substitute(log_file=log_file))
|
2015-04-30 09:52:57 -06:00
|
|
|
|
2016-06-16 04:06:12 -06:00
|
|
|
|
2021-12-01 05:28:23 -07:00
|
|
|
def _setup_stdlib_logging(
|
|
|
|
config: "HomeServerConfig", log_config_path: Optional[str], logBeginner: LogBeginner
|
|
|
|
) -> None:
|
2019-08-28 05:18:53 -06:00
|
|
|
"""
|
2020-10-29 05:27:37 -06:00
|
|
|
Set up Python standard library logging.
|
2017-03-10 08:16:50 -07:00
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2020-08-07 04:39:29 -06: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 12:19:47 -07:00
|
|
|
log_context_filter = LoggingContextFilter()
|
2021-09-13 11:07:12 -06:00
|
|
|
log_metadata_filter = MetadataFilter({"server_name": config.server.server_name})
|
2020-08-07 04:39:29 -06:00
|
|
|
old_factory = logging.getLogRecordFactory()
|
|
|
|
|
2021-12-01 05:28:23 -07:00
|
|
|
def factory(*args: Any, **kwargs: Any) -> logging.LogRecord:
|
2020-08-07 04:39:29 -06:00
|
|
|
record = old_factory(*args, **kwargs)
|
2020-10-29 05:27:37 -06:00
|
|
|
log_context_filter.filter(record)
|
|
|
|
log_metadata_filter.filter(record)
|
2020-08-07 04:39:29 -06:00
|
|
|
return record
|
|
|
|
|
|
|
|
logging.setLogRecordFactory(factory)
|
|
|
|
|
2023-02-03 06:27:31 -07:00
|
|
|
# Configure the logger with the initial configuration.
|
|
|
|
if log_config_path is None:
|
|
|
|
log_format = (
|
|
|
|
"%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s"
|
|
|
|
" - %(message)s"
|
|
|
|
)
|
|
|
|
|
|
|
|
logger = logging.getLogger("")
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
logging.getLogger("synapse.storage.SQL").setLevel(logging.INFO)
|
|
|
|
|
|
|
|
formatter = logging.Formatter(log_format)
|
|
|
|
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
else:
|
|
|
|
# Load the logging configuration.
|
|
|
|
_load_logging_config(log_config_path)
|
|
|
|
|
2019-08-28 05:18:53 -06:00
|
|
|
# Route Twisted's native logging through to the standard library logging
|
|
|
|
# system.
|
2016-12-30 04:05:37 -07:00
|
|
|
observer = STDLibLogObserver()
|
2018-09-13 03:59:32 -06:00
|
|
|
|
2020-09-07 09:54:30 -06:00
|
|
|
threadlocal = threading.local()
|
2018-09-13 03:59:32 -06:00
|
|
|
|
2021-03-09 05:41:32 -07:00
|
|
|
@implementer(ILogObserver)
|
|
|
|
def _log(event: dict) -> None:
|
2018-09-13 03:59:32 -06: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 09:54:30 -06: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 03:59:32 -06:00
|
|
|
|
2021-06-21 04:41:25 -06:00
|
|
|
logBeginner.beginLoggingTo([_log], redirectStandardIO=False)
|
2019-08-28 05:18:53 -06:00
|
|
|
|
|
|
|
|
2020-10-29 05:27:37 -06: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 05:18:53 -06:00
|
|
|
|
|
|
|
if not log_config:
|
2020-10-29 05:27:37 -06:00
|
|
|
logging.warning("Loaded a blank logging config?")
|
|
|
|
|
2022-02-17 06:32:18 -07:00
|
|
|
# If the old structured logging configuration is being used, raise an error.
|
2020-10-29 05:27:37 -06:00
|
|
|
if "structured" in log_config and log_config.get("structured"):
|
2022-02-17 06:32:18 -07:00
|
|
|
raise ConfigError(STRUCTURED_ERROR)
|
2019-08-28 05:18:53 -06:00
|
|
|
|
|
|
|
logging.config.dictConfig(log_config)
|
|
|
|
|
2023-02-03 06:27:31 -07:00
|
|
|
# Blow away the pyo3-log cache so that it reloads the configuration.
|
|
|
|
reset_logging_config()
|
|
|
|
|
2019-08-28 05:18:53 -06:00
|
|
|
|
2021-12-01 05:28:23 -07:00
|
|
|
def _reload_logging_config(log_config_path: Optional[str]) -> None:
|
2020-10-29 05:27:37 -06: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 10:29:55 -06:00
|
|
|
def setup_logging(
|
2021-10-22 11:15:41 -06:00
|
|
|
hs: "HomeServer",
|
2021-12-01 05:28:23 -07:00
|
|
|
config: "HomeServerConfig",
|
|
|
|
use_worker_options: bool = False,
|
2021-10-22 11:15:41 -06:00
|
|
|
logBeginner: LogBeginner = globalLogBeginner,
|
2020-10-29 05:27:37 -06:00
|
|
|
) -> None:
|
2019-08-28 05:18:53 -06:00
|
|
|
"""
|
|
|
|
Set up the logging subsystem.
|
|
|
|
|
|
|
|
Args:
|
2022-11-16 08:25:24 -07:00
|
|
|
config: configuration data
|
2019-08-28 05:18:53 -06:00
|
|
|
|
2022-11-16 08:25:24 -07:00
|
|
|
use_worker_options: True to use the 'worker_log_config' option
|
2019-08-28 05:18:53 -06:00
|
|
|
instead of 'log_config'.
|
2019-09-12 10:29:55 -06:00
|
|
|
|
|
|
|
logBeginner: The Twisted logBeginner to use.
|
|
|
|
|
2019-08-28 05:18:53 -06:00
|
|
|
"""
|
2022-10-07 08:19:59 -06:00
|
|
|
from twisted.internet import reactor
|
|
|
|
|
2020-10-29 05:27:37 -06:00
|
|
|
log_config_path = (
|
2021-09-24 05:25:21 -06:00
|
|
|
config.worker.worker_log_config
|
|
|
|
if use_worker_options
|
|
|
|
else config.logging.log_config
|
2020-10-29 05:27:37 -06:00
|
|
|
)
|
2019-08-28 05:18:53 -06:00
|
|
|
|
2020-10-29 05:27:37 -06: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 12:20:44 -06:00
|
|
|
from synapse.app import _base as appbase
|
|
|
|
|
2020-10-29 05:27:37 -06:00
|
|
|
appbase.register_sighup(_reload_logging_config, log_config_path)
|
2019-08-28 05:18:53 -06:00
|
|
|
|
2020-10-29 05:27:37 -06:00
|
|
|
# Log immediately so we can grep backwards.
|
2019-11-01 06:03:44 -06:00
|
|
|
logging.warning("***** STARTING SERVER *****")
|
2022-02-14 06:12:22 -07:00
|
|
|
logging.warning(
|
|
|
|
"Server %s version %s",
|
|
|
|
sys.argv[0],
|
2022-06-07 08:24:11 -06:00
|
|
|
SYNAPSE_VERSION,
|
2022-02-14 06:12:22 -07:00
|
|
|
)
|
2021-09-13 11:07:12 -06:00
|
|
|
logging.info("Server hostname: %s", config.server.server_name)
|
2020-05-22 09:11:35 -06:00
|
|
|
logging.info("Instance name: %s", hs.get_instance_name())
|
2022-10-07 08:19:59 -06:00
|
|
|
logging.info("Twisted reactor: %s", type(reactor).__name__)
|