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

Support outputting structured logs in addition to standard logs #8607

Merged
merged 23 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2db1099
Remove the structured logging configuration code.
clokep Oct 21, 2020
509594f
Remove an unnecessary return value.
clokep Oct 20, 2020
99f50ec
Rework structured logging to use the Python standard library logging …
clokep Oct 21, 2020
fcb74ae
Pipe through the server_name properly.
clokep Oct 21, 2020
24a9882
Use a standard library logger.
clokep Oct 21, 2020
4462758
Update synmark for the changes.
clokep Oct 21, 2020
24ab2df
Update logging format
clokep Oct 26, 2020
5fbc11c
Fix-up formatting using __all__.
clokep Oct 26, 2020
b2fc88b
Do not build an unnecessary set.
clokep Oct 26, 2020
650bb09
Stop using the DEBUG decorators.
clokep Oct 26, 2020
7115697
Raise an error if structured is in the logging config.
clokep Oct 26, 2020
e855dbb
__all__ takes strings, not objects.
clokep Oct 26, 2020
7071d89
Update the sample config.
clokep Oct 26, 2020
6b785c1
Revamp tests a bit to avoid impacting other tests.
clokep Oct 26, 2020
8d51476
Abstract handling of loggers in tests.
clokep Oct 27, 2020
7fb5505
Add a test for including additional structured data.
clokep Oct 27, 2020
11a488c
Lint.
clokep Oct 27, 2020
a19c967
Fix test after rename.
clokep Oct 27, 2020
e98a6d1
Add an upgrade note.
clokep Oct 27, 2020
babdd5b
Rework the code to load logging configs.
clokep Oct 28, 2020
10738cc
Convert legacy drain configurations to standard library handler configs.
clokep Oct 28, 2020
f801d71
Add back a JSON formatter without time.
clokep Oct 28, 2020
1c0181a
Fix type hints.
clokep Oct 28, 2020
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
16 changes: 16 additions & 0 deletions UPGRADE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ for example:
wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb

Upgrading to v1.23.0
====================

Structured logging configuration breaking changes
-------------------------------------------------

This release deprecates use of the ``structured: true`` logging configuration for
structured logging. If your logging configuration contains ``structured: true``
then it should be modified based on the `structured logging documentation
<https://github.com/matrix-org/synapse/blob/master/docs/structured_logging.md>`_.

The ``structured`` and ``drains`` logging options are now deprecated and should
be replaced by standard logging configuration of ``handlers`` and ``formatters`.

A future will release of Synapse will make using ``structured: true`` an error.

Upgrading to v1.22.0
====================

Expand Down
1 change: 1 addition & 0 deletions changelog.d/8607.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-organize the structured logging code to separate the TCP transport handling from the JSON formatting.
4 changes: 4 additions & 0 deletions docs/sample_log_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
# This is a YAML file containing a standard Python logging configuration
# dictionary. See [1] for details on the valid settings.
#
# Synapse also supports structured logging for machine readable logs which can
# be ingested by ELK stacks. See [2] for details.
#
# [1]: https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
# [2]: https://github.com/matrix-org/synapse/blob/master/docs/structured_logging.md

version: 1

Expand Down
164 changes: 121 additions & 43 deletions docs/structured_logging.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,161 @@
# Structured Logging

A structured logging system can be useful when your logs are destined for a machine to parse and process. By maintaining its machine-readable characteristics, it enables more efficient searching and aggregations when consumed by software such as the "ELK stack".
A structured logging system can be useful when your logs are destined for a
machine to parse and process. By maintaining its machine-readable characteristics,
it enables more efficient searching and aggregations when consumed by software
such as the "ELK stack".

Synapse's structured logging system is configured via the file that Synapse's `log_config` config option points to. The file must be YAML and contain `structured: true`. It must contain a list of "drains" (places where logs go to).
Synapse's structured logging system is configured via the file that Synapse's
`log_config` config option points to. The file should include a formatter which
uses the `synapse.logging.TerseJsonFormatter` class included with Synapse and a
handler which uses the above formatter.

There is also a `synapse.logging.JsonFormatter` option which does not include
a timestamp in the resulting JSON. This is useful if the log ingester adds its
own timestamp.

A structured logging configuration looks similar to the following:

```yaml
structured: true
version: 1

formatters:
structured:
class: synapse.logging.TerseJsonFormatter

handlers:
file:
class: logging.handlers.TimedRotatingFileHandler
formatter: structured
filename: /path/to/my/logs/homeserver.log
when: midnight
backupCount: 3 # Does not include the current log file.
encoding: utf8

loggers:
synapse:
level: INFO
handlers: [remote]
synapse.storage.SQL:
level: WARNING

drains:
console:
type: console
location: stdout
file:
type: file_json
location: homeserver.log
```

The above logging config will set Synapse as 'INFO' logging level by default, with the SQL layer at 'WARNING', and will have two logging drains (to the console and to a file, stored as JSON).

## Drain Types
The above logging config will set Synapse as 'INFO' logging level by default,
with the SQL layer at 'WARNING', and will log to a file, stored as JSON.

Drain types can be specified by the `type` key.
It is also possible to figure Synapse to log to a remote endpoint by using the
`synapse.logging.RemoteHandler` class included with Synapse. It takes the
following arguments:

### `console`
- `host`: Hostname or IP address of the log aggregator.
- `port`: Numerical port to contact on the host.
- `maximum_buffer`: (Optional, defaults to 1000) The maximum buffer size to allow.

Outputs human-readable logs to the console.
A remote structured logging configuration looks similar to the following:

Arguments:
```yaml
version: 1

- `location`: Either `stdout` or `stderr`.
formatters:
structured:
class: synapse.logging.TerseJsonFormatter

### `console_json`
handlers:
remote:
class: synapse.logging.RemoteHandler
formatter: structured
host: 10.1.2.3
port: 9999

Outputs machine-readable JSON logs to the console.
loggers:
synapse:
level: INFO
handlers: [remote]
synapse.storage.SQL:
level: WARNING
```

Arguments:
The above logging config will set Synapse as 'INFO' logging level by default,
with the SQL layer at 'WARNING', and will log JSON formatted messages to a
remote endpoint at 10.1.2.3:9999.

- `location`: Either `stdout` or `stderr`.
## Upgrading from legacy structured logging configuration

### `console_json_terse`
Versions of Synapse prior to v1.23.0 included a custom structured logging
configuration which is deprecated. It used a `structured: true` flag and
configured `drains` instead of ``handlers`` and `formatters`.

Outputs machine-readable JSON logs to the console, separated by newlines. This
format is not designed to be read and re-formatted into human-readable text, but
is optimal for a logging aggregation system.
Synapse currently automatically converts the old configuration to the new
configuration, but this will be removed in a future version of Synapse. The
following reference can be used to update your configuration. Based on the drain
`type`, we can pick a new handler:

Arguments:
1. For a type of `console`, `console_json`, or `console_json_terse`: a handler
with a class of `logging.StreamHandler` and a `stream` of `ext://sys.stdout`
or `ext://sys.stderr` should be used.
2. For a type of `file` or `file_json`: a handler of `logging.FileHandler` with
a location of the file path should be used.
3. For a type of `network_json_terse`: a handler of `synapse.logging.RemoteHandler`
with the host and port should be used.

- `location`: Either `stdout` or `stderr`.
Then based on the drain `type` we can pick a new formatter:

### `file`
1. For a type of `console` or `file` no formatter is necessary.
2. For a type of `console_json` or `file_json`: a formatter of
`synapse.logging.JsonFormatter` should be used.
3. For a type of `console_json_terse` or `network_json_terse`: a formatter of
`synapse.logging.TerseJsonFormatter` should be used.

Outputs human-readable logs to a file.
For each new handler and formatter they should be added to the logging configuration
and then assigned to either a logger or the root logger.

Arguments:
An example legacy configuration:

- `location`: An absolute path to the file to log to.
```yaml
structured: true

### `file_json`
loggers:
synapse:
level: INFO
synapse.storage.SQL:
level: WARNING

Outputs machine-readable logs to a file.
drains:
console:
type: console
location: stdout
file:
type: file_json
location: homeserver.log
```

Arguments:
Would be converted into a new configuration:

- `location`: An absolute path to the file to log to.
```yaml
version: 1

### `network_json_terse`
formatters:
json:
class: synapse.logging.JsonFormatter

Delivers machine-readable JSON logs to a log aggregator over TCP. This is
compatible with LogStash's TCP input with the codec set to `json_lines`.
handlers:
console:
class: logging.StreamHandler
location: ext://sys.stdout
file:
class: logging.FileHandler
formatter: json
filename: homeserver.log

Arguments:
loggers:
synapse:
level: INFO
handlers: [console, file]
synapse.storage.SQL:
level: WARNING
```

- `host`: Hostname or IP address of the log aggregator.
- `port`: Numerical port to contact on the host.
The new logging configuration is a bit more verbose, but significantly more
flexible. It allows for configuration that were not previously possible, such as
sending plain logs over the network, or using different handlers for different
modules.
2 changes: 1 addition & 1 deletion scripts-dev/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ else
# then lint everything!
if [[ -z ${files+x} ]]; then
# Lint all source code files and directories
files=("synapse" "tests" "scripts-dev" "scripts" "contrib" "synctl" "setup.py")
files=("synapse" "tests" "scripts-dev" "scripts" "contrib" "synctl" "setup.py" "synmark")
fi
fi

Expand Down
Loading