|
1 | 1 | # Structured Logging
|
2 | 2 |
|
3 |
| -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". |
| 3 | +A structured logging system can be useful when your logs are destined for a |
| 4 | +machine to parse and process. By maintaining its machine-readable characteristics, |
| 5 | +it enables more efficient searching and aggregations when consumed by software |
| 6 | +such as the "ELK stack". |
4 | 7 |
|
5 |
| -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). |
| 8 | +Synapse's structured logging system is configured via the file that Synapse's |
| 9 | +`log_config` config option points to. The file should include a formatter which |
| 10 | +uses the `synapse.logging.TerseJsonFormatter` class included with Synapse and a |
| 11 | +handler which uses the above formatter. |
| 12 | + |
| 13 | +There is also a `synapse.logging.JsonFormatter` option which does not include |
| 14 | +a timestamp in the resulting JSON. This is useful if the log ingester adds its |
| 15 | +own timestamp. |
6 | 16 |
|
7 | 17 | A structured logging configuration looks similar to the following:
|
8 | 18 |
|
9 | 19 | ```yaml
|
10 |
| -structured: true |
| 20 | +version: 1 |
| 21 | + |
| 22 | +formatters: |
| 23 | + structured: |
| 24 | + class: synapse.logging.TerseJsonFormatter |
| 25 | + |
| 26 | +handlers: |
| 27 | + file: |
| 28 | + class: logging.handlers.TimedRotatingFileHandler |
| 29 | + formatter: structured |
| 30 | + filename: /path/to/my/logs/homeserver.log |
| 31 | + when: midnight |
| 32 | + backupCount: 3 # Does not include the current log file. |
| 33 | + encoding: utf8 |
11 | 34 |
|
12 | 35 | loggers:
|
13 | 36 | synapse:
|
14 | 37 | level: INFO
|
| 38 | + handlers: [remote] |
15 | 39 | synapse.storage.SQL:
|
16 | 40 | level: WARNING
|
17 |
| - |
18 |
| -drains: |
19 |
| - console: |
20 |
| - type: console |
21 |
| - location: stdout |
22 |
| - file: |
23 |
| - type: file_json |
24 |
| - location: homeserver.log |
25 | 41 | ```
|
26 | 42 |
|
27 |
| -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). |
28 |
| -
|
29 |
| -## Drain Types |
| 43 | +The above logging config will set Synapse as 'INFO' logging level by default, |
| 44 | +with the SQL layer at 'WARNING', and will log to a file, stored as JSON. |
30 | 45 |
|
31 |
| -Drain types can be specified by the `type` key. |
| 46 | +It is also possible to figure Synapse to log to a remote endpoint by using the |
| 47 | +`synapse.logging.RemoteHandler` class included with Synapse. It takes the |
| 48 | +following arguments: |
32 | 49 |
|
33 |
| -### `console` |
| 50 | +- `host`: Hostname or IP address of the log aggregator. |
| 51 | +- `port`: Numerical port to contact on the host. |
| 52 | +- `maximum_buffer`: (Optional, defaults to 1000) The maximum buffer size to allow. |
34 | 53 |
|
35 |
| -Outputs human-readable logs to the console. |
| 54 | +A remote structured logging configuration looks similar to the following: |
36 | 55 |
|
37 |
| -Arguments: |
| 56 | +```yaml |
| 57 | +version: 1 |
38 | 58 |
|
39 |
| -- `location`: Either `stdout` or `stderr`. |
| 59 | +formatters: |
| 60 | + structured: |
| 61 | + class: synapse.logging.TerseJsonFormatter |
40 | 62 |
|
41 |
| -### `console_json` |
| 63 | +handlers: |
| 64 | + remote: |
| 65 | + class: synapse.logging.RemoteHandler |
| 66 | + formatter: structured |
| 67 | + host: 10.1.2.3 |
| 68 | + port: 9999 |
42 | 69 |
|
43 |
| -Outputs machine-readable JSON logs to the console. |
| 70 | +loggers: |
| 71 | + synapse: |
| 72 | + level: INFO |
| 73 | + handlers: [remote] |
| 74 | + synapse.storage.SQL: |
| 75 | + level: WARNING |
| 76 | +``` |
44 | 77 |
|
45 |
| -Arguments: |
| 78 | +The above logging config will set Synapse as 'INFO' logging level by default, |
| 79 | +with the SQL layer at 'WARNING', and will log JSON formatted messages to a |
| 80 | +remote endpoint at 10.1.2.3:9999. |
46 | 81 |
|
47 |
| -- `location`: Either `stdout` or `stderr`. |
| 82 | +## Upgrading from legacy structured logging configuration |
48 | 83 |
|
49 |
| -### `console_json_terse` |
| 84 | +Versions of Synapse prior to v1.23.0 included a custom structured logging |
| 85 | +configuration which is deprecated. It used a `structured: true` flag and |
| 86 | +configured `drains` instead of ``handlers`` and `formatters`. |
50 | 87 |
|
51 |
| -Outputs machine-readable JSON logs to the console, separated by newlines. This |
52 |
| -format is not designed to be read and re-formatted into human-readable text, but |
53 |
| -is optimal for a logging aggregation system. |
| 88 | +Synapse currently automatically converts the old configuration to the new |
| 89 | +configuration, but this will be removed in a future version of Synapse. The |
| 90 | +following reference can be used to update your configuration. Based on the drain |
| 91 | +`type`, we can pick a new handler: |
54 | 92 |
|
55 |
| -Arguments: |
| 93 | +1. For a type of `console`, `console_json`, or `console_json_terse`: a handler |
| 94 | + with a class of `logging.StreamHandler` and a `stream` of `ext://sys.stdout` |
| 95 | + or `ext://sys.stderr` should be used. |
| 96 | +2. For a type of `file` or `file_json`: a handler of `logging.FileHandler` with |
| 97 | + a location of the file path should be used. |
| 98 | +3. For a type of `network_json_terse`: a handler of `synapse.logging.RemoteHandler` |
| 99 | + with the host and port should be used. |
56 | 100 |
|
57 |
| -- `location`: Either `stdout` or `stderr`. |
| 101 | +Then based on the drain `type` we can pick a new formatter: |
58 | 102 |
|
59 |
| -### `file` |
| 103 | +1. For a type of `console` or `file` no formatter is necessary. |
| 104 | +2. For a type of `console_json` or `file_json`: a formatter of |
| 105 | + `synapse.logging.JsonFormatter` should be used. |
| 106 | +3. For a type of `console_json_terse` or `network_json_terse`: a formatter of |
| 107 | + `synapse.logging.TerseJsonFormatter` should be used. |
60 | 108 |
|
61 |
| -Outputs human-readable logs to a file. |
| 109 | +For each new handler and formatter they should be added to the logging configuration |
| 110 | +and then assigned to either a logger or the root logger. |
62 | 111 |
|
63 |
| -Arguments: |
| 112 | +An example legacy configuration: |
64 | 113 |
|
65 |
| -- `location`: An absolute path to the file to log to. |
| 114 | +```yaml |
| 115 | +structured: true |
66 | 116 |
|
67 |
| -### `file_json` |
| 117 | +loggers: |
| 118 | + synapse: |
| 119 | + level: INFO |
| 120 | + synapse.storage.SQL: |
| 121 | + level: WARNING |
68 | 122 |
|
69 |
| -Outputs machine-readable logs to a file. |
| 123 | +drains: |
| 124 | + console: |
| 125 | + type: console |
| 126 | + location: stdout |
| 127 | + file: |
| 128 | + type: file_json |
| 129 | + location: homeserver.log |
| 130 | +``` |
70 | 131 |
|
71 |
| -Arguments: |
| 132 | +Would be converted into a new configuration: |
72 | 133 |
|
73 |
| -- `location`: An absolute path to the file to log to. |
| 134 | +```yaml |
| 135 | +version: 1 |
74 | 136 |
|
75 |
| -### `network_json_terse` |
| 137 | +formatters: |
| 138 | + json: |
| 139 | + class: synapse.logging.JsonFormatter |
76 | 140 |
|
77 |
| -Delivers machine-readable JSON logs to a log aggregator over TCP. This is |
78 |
| -compatible with LogStash's TCP input with the codec set to `json_lines`. |
| 141 | +handlers: |
| 142 | + console: |
| 143 | + class: logging.StreamHandler |
| 144 | + location: ext://sys.stdout |
| 145 | + file: |
| 146 | + class: logging.FileHandler |
| 147 | + formatter: json |
| 148 | + filename: homeserver.log |
79 | 149 |
|
80 |
| -Arguments: |
| 150 | +loggers: |
| 151 | + synapse: |
| 152 | + level: INFO |
| 153 | + handlers: [console, file] |
| 154 | + synapse.storage.SQL: |
| 155 | + level: WARNING |
| 156 | +``` |
81 | 157 |
|
82 |
| -- `host`: Hostname or IP address of the log aggregator. |
83 |
| -- `port`: Numerical port to contact on the host. |
| 158 | +The new logging configuration is a bit more verbose, but significantly more |
| 159 | +flexible. It allows for configuration that were not previously possible, such as |
| 160 | +sending plain logs over the network, or using different handlers for different |
| 161 | +modules. |
0 commit comments