Skip to content

Commit 6b6d7fc

Browse files
authored
Merge pull request freqtrade#11505 from freqtrade/feat/log_from_config
allow loading logging from config
2 parents c6959c9 + 24e94cf commit 6b6d7fc

12 files changed

+567
-90
lines changed

build_helpers/schema.json

+28
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@
542542
"description": "Edge configuration.",
543543
"$ref": "#/definitions/edge"
544544
},
545+
"log_config": {
546+
"description": "Logging configuration.",
547+
"$ref": "#/definitions/logging"
548+
},
545549
"freqai": {
546550
"description": "FreqAI configuration.",
547551
"$ref": "#/definitions/freqai"
@@ -1281,6 +1285,30 @@
12811285
"allowed_risk"
12821286
]
12831287
},
1288+
"logging": {
1289+
"type": "object",
1290+
"properties": {
1291+
"version": {
1292+
"type": "number",
1293+
"const": 1
1294+
},
1295+
"formatters": {
1296+
"type": "object"
1297+
},
1298+
"handlers": {
1299+
"type": "object"
1300+
},
1301+
"root": {
1302+
"type": "object"
1303+
}
1304+
},
1305+
"required": [
1306+
"version",
1307+
"formatters",
1308+
"handlers",
1309+
"root"
1310+
]
1311+
},
12841312
"external_message_consumer": {
12851313
"description": "Configuration for external message consumer.",
12861314
"type": "object",

docs/advanced-setup.md

+200-15
Original file line numberDiff line numberDiff line change
@@ -188,30 +188,111 @@ as the watchdog.
188188

189189
## Advanced Logging
190190

191-
On many Linux systems the bot can be configured to send its log messages to `syslog` or `journald` system services. Logging to a remote `syslog` server is also available on Windows. The special values for the `--logfile` command line option can be used for this.
191+
Freqtrade uses the default logging module provided by python.
192+
Python allows for extensive [logging configuration](https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig) in this regards - way more than what can be covered here.
193+
194+
Default logging (Colored terminal output) is setup by default if no `log_config` is provided.
195+
Using `--logfile logfile.log` will enable the RotatingFileHandler.
196+
If you're not content with the log format - or with the default settings provided for the RotatingFileHandler, you can customize logging to your liking.
197+
198+
The default configuration looks roughly like the below - with the file handler being provided - but not enabled.
199+
200+
``` json hl_lines="5-7 13-16 27"
201+
{
202+
"log_config": {
203+
"version": 1,
204+
"formatters": {
205+
"basic": {
206+
"format": "%(message)s"
207+
},
208+
"standard": {
209+
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
210+
}
211+
},
212+
"handlers": {
213+
"console": {
214+
"class": "freqtrade.loggers.ft_rich_handler.FtRichHandler",
215+
"formatter": "basic"
216+
},
217+
"file": {
218+
"class": "logging.handlers.RotatingFileHandler",
219+
"formatter": "standard",
220+
// "filename": "someRandomLogFile.log",
221+
"maxBytes": 10485760,
222+
"backupCount": 10
223+
}
224+
},
225+
"root": {
226+
"handlers": [
227+
"console",
228+
// "file"
229+
],
230+
"level": "INFO",
231+
}
232+
}
233+
}
234+
```
192235

193-
### Logging to syslog
236+
!!! Note "highlighted lines"
237+
Highlighted lines in the above code-block define the Rich handler and belong together.
238+
The formatter "standard" and "file" will belong to the FileHandler.
194239

195-
To send Freqtrade log messages to a local or remote `syslog` service use the `--logfile` command line option with the value in the following format:
240+
Each handler must use one of the defined formatters (by name) - and it's class must be available and a valid logging class.
241+
To actually use a handler - it must be in the "handlers" section inside the "root" segment.
242+
If this section is left out, freqtrade will provide no output (in the non-configured handler, anyway).
196243

197-
* `--logfile syslog:<syslog_address>` -- send log messages to `syslog` service using the `<syslog_address>` as the syslog address.
244+
!!! Tip "Explicit log configuration"
245+
We recommend to extract the logging configuration from your main configuration, and provide it to your bot via [multiple configuration files](configuration.md#multiple-configuration-files) functionality. This will avoid unnecessary code duplication.
198246

199-
The syslog address can be either a Unix domain socket (socket filename) or a UDP socket specification, consisting of IP address and UDP port, separated by the `:` character.
247+
---
248+
249+
On many Linux systems the bot can be configured to send its log messages to `syslog` or `journald` system services. Logging to a remote `syslog` server is also available on Windows. The special values for the `--logfile` command line option can be used for this.
200250

201-
So, the following are the examples of possible usages:
251+
### Logging to syslog
252+
253+
To send Freqtrade log messages to a local or remote `syslog` service use the `"log_config"` setup option to configure logging.
254+
255+
``` json
256+
{
257+
// ...
258+
"log_config": {
259+
"version": 1,
260+
"formatters": {
261+
"syslog_fmt": {
262+
"format": "%(name)s - %(levelname)s - %(message)s"
263+
}
264+
},
265+
"handlers": {
266+
// Other handlers?
267+
"syslog": {
268+
"class": "logging.handlers.SysLogHandler",
269+
"formatter": "syslog_fmt",
270+
// Use one of the other options above as address instead?
271+
"address": "/dev/log"
272+
}
273+
},
274+
"root": {
275+
"handlers": [
276+
// other handlers
277+
"syslog",
278+
279+
]
280+
}
281+
282+
}
283+
}
284+
```
285+
286+
[Additional log-handlers](#advanced-logging) may need to be configured to for example also have log output in the console.
202287

203-
* `--logfile syslog:/dev/log` -- log to syslog (rsyslog) using the `/dev/log` socket, suitable for most systems.
204-
* `--logfile syslog` -- same as above, the shortcut for `/dev/log`.
205-
* `--logfile syslog:/var/run/syslog` -- log to syslog (rsyslog) using the `/var/run/syslog` socket. Use this on MacOS.
206-
* `--logfile syslog:localhost:514` -- log to local syslog using UDP socket, if it listens on port 514.
207-
* `--logfile syslog:<ip>:514` -- log to remote syslog at IP address and port 514. This may be used on Windows for remote logging to an external syslog server.
288+
#### Syslog usage
208289

209290
Log messages are send to `syslog` with the `user` facility. So you can see them with the following commands:
210291

211-
* `tail -f /var/log/user`, or
292+
* `tail -f /var/log/user`, or
212293
* install a comprehensive graphical viewer (for instance, 'Log File Viewer' for Ubuntu).
213294

214-
On many systems `syslog` (`rsyslog`) fetches data from `journald` (and vice versa), so both `--logfile syslog` or `--logfile journald` can be used and the messages be viewed with both `journalctl` and a syslog viewer utility. You can combine this in any way which suites you better.
295+
On many systems `syslog` (`rsyslog`) fetches data from `journald` (and vice versa), so both syslog or journald can be used and the messages be viewed with both `journalctl` and a syslog viewer utility. You can combine this in any way which suites you better.
215296

216297
For `rsyslog` the messages from the bot can be redirected into a separate dedicated log file. To achieve this, add
217298

@@ -228,13 +309,69 @@ For `syslog` (`rsyslog`), the reduction mode can be switched on. This will reduc
228309
$RepeatedMsgReduction on
229310
```
230311
312+
#### Syslog addressing
313+
314+
The syslog address can be either a Unix domain socket (socket filename) or a UDP socket specification, consisting of IP address and UDP port, separated by the `:` character.
315+
316+
317+
So, the following are the examples of possible addresses:
318+
319+
* `"address": "/dev/log"` -- log to syslog (rsyslog) using the `/dev/log` socket, suitable for most systems.
320+
* `"address": "/var/run/syslog"` -- log to syslog (rsyslog) using the `/var/run/syslog` socket. Use this on MacOS.
321+
* `"address": "localhost:514"` -- log to local syslog using UDP socket, if it listens on port 514.
322+
* `"address": "<ip>:514"` -- log to remote syslog at IP address and port 514. This may be used on Windows for remote logging to an external syslog server.
323+
324+
325+
??? Info "Deprecated - configure syslog via command line"
326+
327+
`--logfile syslog:<syslog_address>` -- send log messages to `syslog` service using the `<syslog_address>` as the syslog address.
328+
329+
The syslog address can be either a Unix domain socket (socket filename) or a UDP socket specification, consisting of IP address and UDP port, separated by the `:` character.
330+
331+
So, the following are the examples of possible usages:
332+
333+
* `--logfile syslog:/dev/log` -- log to syslog (rsyslog) using the `/dev/log` socket, suitable for most systems.
334+
* `--logfile syslog` -- same as above, the shortcut for `/dev/log`.
335+
* `--logfile syslog:/var/run/syslog` -- log to syslog (rsyslog) using the `/var/run/syslog` socket. Use this on MacOS.
336+
* `--logfile syslog:localhost:514` -- log to local syslog using UDP socket, if it listens on port 514.
337+
* `--logfile syslog:<ip>:514` -- log to remote syslog at IP address and port 514. This may be used on Windows for remote logging to an external syslog server.
338+
231339
### Logging to journald
232340
233341
This needs the `cysystemd` python package installed as dependency (`pip install cysystemd`), which is not available on Windows. Hence, the whole journald logging functionality is not available for a bot running on Windows.
234342
235-
To send Freqtrade log messages to `journald` system service use the `--logfile` command line option with the value in the following format:
343+
To send Freqtrade log messages to `journald` system service, add the following configuration snippet to your configuration.
344+
345+
``` json
346+
{
347+
// ...
348+
"log_config": {
349+
"version": 1,
350+
"formatters": {
351+
"journald_fmt": {
352+
"format": "%(name)s - %(levelname)s - %(message)s"
353+
}
354+
},
355+
"handlers": {
356+
// Other handlers?
357+
"journald": {
358+
"class": "cysystemd.journal.JournaldLogHandler",
359+
"formatter": "journald_fmt",
360+
}
361+
},
362+
"root": {
363+
"handlers": [
364+
// ..
365+
"journald",
366+
367+
]
368+
}
369+
370+
}
371+
}
372+
```
236373

237-
* `--logfile journald` -- send log messages to `journald`.
374+
[Additional log-handlers](#advanced-logging) may need to be configured to for example also have log output in the console.
238375

239376
Log messages are send to `journald` with the `user` facility. So you can see them with the following commands:
240377

@@ -244,3 +381,51 @@ Log messages are send to `journald` with the `user` facility. So you can see the
244381
There are many other options in the `journalctl` utility to filter the messages, see manual pages for this utility.
245382

246383
On many systems `syslog` (`rsyslog`) fetches data from `journald` (and vice versa), so both `--logfile syslog` or `--logfile journald` can be used and the messages be viewed with both `journalctl` and a syslog viewer utility. You can combine this in any way which suites you better.
384+
385+
??? Info "Deprecated - configure journald via command line"
386+
To send Freqtrade log messages to `journald` system service use the `--logfile` command line option with the value in the following format:
387+
388+
`--logfile journald` -- send log messages to `journald`.
389+
390+
### Log format as JSON
391+
392+
You can also configure the default output stream to use JSON format instead.
393+
The "fmt_dict" attribute defines the keys for the json output - as well as the [python logging LogRecord attributes](https://docs.python.org/3/library/logging.html#logrecord-attributes).
394+
395+
The below configuration will change the default output to JSON. The same formatter could however also be used in combination with the `RotatingFileHandler`.
396+
We recommend to keep one format in human readable form.
397+
398+
``` json
399+
{
400+
// ...
401+
"log_config": {
402+
"version": 1,
403+
"formatters": {
404+
"json": {
405+
"()": "freqtrade.loggers.json_formatter.JsonFormatter",
406+
"fmt_dict": {
407+
"timestamp": "asctime",
408+
"level": "levelname",
409+
"logger": "name",
410+
"message": "message"
411+
}
412+
}
413+
},
414+
"handlers": {
415+
// Other handlers?
416+
"jsonStream": {
417+
"class": "logging.StreamHandler",
418+
"formatter": "json"
419+
}
420+
},
421+
"root": {
422+
"handlers": [
423+
// ..
424+
"jsonStream",
425+
426+
]
427+
}
428+
429+
}
430+
}
431+
```

docs/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi
282282
| `dataformat_ohlcv` | Data format to use to store historical candle (OHLCV) data. <br> *Defaults to `feather`*. <br> **Datatype:** String
283283
| `dataformat_trades` | Data format to use to store historical trades data. <br> *Defaults to `feather`*. <br> **Datatype:** String
284284
| `reduce_df_footprint` | Recast all numeric columns to float32/int32, with the objective of reducing ram/disk usage (and decreasing train/inference timing in FreqAI). (Currently only affects FreqAI use-cases) <br> **Datatype:** Boolean. <br> Default: `False`.
285+
| `log_config` | Dictionary containing the log config for python logging. [more info](advanced-setup.md#advanced-logging) <br> **Datatype:** dict. <br> Default: `FtRichHandler`
285286

286287
### Parameters in the strategy
287288

docs/deprecated.md

+5
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ Setting protections from the configuration via `"protections": [],` has been rem
8888
Using hdf5 as data storage has been deprecated in 2024.12 and was removed in 2025.1. We recommend switching to the feather data format.
8989

9090
Please use the [`convert-data` subcommand](data-download.md#sub-command-convert-data) to convert your existing data to one of the supported formats before updating.
91+
92+
## Configuring advanced logging via config
93+
94+
Configuring syslog and journald via `--logfile systemd` and `--logfile journald` respectively has been deprecated in 2025.3.
95+
Please use configuration based [log setup](advanced-setup.md#advanced-logging) instead.

freqtrade/configuration/config_schema.py

+26
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@
425425
"description": "Edge configuration.",
426426
"$ref": "#/definitions/edge",
427427
},
428+
"log_config": {
429+
"description": "Logging configuration.",
430+
"$ref": "#/definitions/logging",
431+
},
428432
"freqai": {
429433
"description": "FreqAI configuration.",
430434
"$ref": "#/definitions/freqai",
@@ -883,6 +887,28 @@
883887
},
884888
"required": ["process_throttle_secs", "allowed_risk"],
885889
},
890+
"logging": {
891+
"type": "object",
892+
"properties": {
893+
"version": {"type": "number", "const": 1},
894+
"formatters": {
895+
"type": "object",
896+
# In theory the below, but can be more flexible
897+
# based on logging.config documentation
898+
# "additionalProperties": {
899+
# "type": "object",
900+
# "properties": {
901+
# "format": {"type": "string"},
902+
# "datefmt": {"type": "string"},
903+
# },
904+
# "required": ["format"],
905+
# },
906+
},
907+
"handlers": {"type": "object"},
908+
"root": {"type": "object"},
909+
},
910+
"required": ["version", "formatters", "handlers", "root"],
911+
},
886912
"external_message_consumer": {
887913
"description": "Configuration for external message consumer.",
888914
"type": "object",

0 commit comments

Comments
 (0)