Skip to content

Commit 5ae12fe

Browse files
Merge pull request #2092 from rabbitmq/rabbitmq-server-2927
Logging: document (key) advanced log formatting options
2 parents bb493d5 + 9732d57 commit 5ae12fe

File tree

4 files changed

+1292
-667
lines changed

4 files changed

+1292
-667
lines changed

docs/logging.md

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This guide covers topics such as:
4848
* [Connection lifecycle events](#logged-events) logged
4949
* [Logging in JSON](#json)
5050
* [Log categories](#log-message-categories)
51+
* [Advanced log formatting](#advanced-formatting)
5152
* How to [inspect service logs](#service-logs) on systemd-based Linux systems
5253
* [Log rotation](#log-rotation)
5354
* [Logging to Syslog](#logging-to-syslog)
@@ -197,7 +198,9 @@ The rest of this guide describes more options, including more advanced ones.
197198
## Log Rotation {#log-rotation}
198199

199200
:::info
201+
200202
When logging to a file, the recommended rotation option is `logrotate`
203+
201204
:::
202205

203206
RabbitMQ nodes always append to the log files, so a complete log history is preserved.
@@ -437,6 +440,11 @@ log.syslog = true
437440
log.syslog.formatter = json
438441
```
439442

443+
Note that [JSON object field mapping can be customized](#json-field-mapping) to
444+
match a specific JSON-based logging format expected by the log collection
445+
tooling.
446+
447+
440448
## Log Message Categories {#log-message-categories}
441449

442450
RabbitMQ has several categories of messages, which can be logged with different
@@ -622,6 +630,207 @@ log.connection.level = info
622630
log.channel.level = info
623631
```
624632

633+
## Advanced Log Format {#advanced-formatting}
634+
635+
This section covers features related to advanced log formatting. These settings are not necessary
636+
in most environments but can be used to adapt RabbitMQ logging to a specific format.
637+
638+
Most examples in this section use the following format:
639+
640+
``` ini
641+
log.file.formatter.level_format = lc4
642+
```
643+
644+
However, the key can also be one of
645+
646+
1. `log.file.formatter.level_format`
647+
2. `log.console.formatter.level_format`
648+
3. `log.exchange.formatter.level_format`
649+
650+
In other words, most settings documented in this section are not specific to a particular
651+
log output, be it `file`, `console` or `exchange`.
652+
653+
### Time Format
654+
655+
Timestamps format can be set to one of the following formats:
656+
657+
1. `rfc3339_space`: the RFC 3339 format with spaces, this is the default format
658+
2. `rfc3339_T`: same as above but with tabs
659+
3. `epoch_usecs`: timestamp (time since UNIX epoch) in microseconds
660+
4. `epoch_secs`: timestamp (time since UNIX epoch) in seconds
661+
662+
``` ini
663+
# this is the default format
664+
log.file.formatter.time_format = rfc3339_space
665+
```
666+
667+
For example, the following format
668+
669+
``` ini
670+
log.file.formatter.time_format = epoch_usecs
671+
```
672+
673+
will result in log messages that look like this:
674+
675+
```
676+
1728025620684139 [info] <0.872.0> started TCP listener on [::]:5672
677+
1728025620687050 [info] <0.892.0> started TLS (SSL) listener on [::]:5671
678+
```
679+
680+
### Log Level Format
681+
682+
[Log level](#log-levels) can be formatted differently:
683+
684+
``` ini
685+
# full value, lower case is the default format
686+
log.file.formatter.level_format = lc
687+
```
688+
689+
``` ini
690+
# use the four character, upper case format
691+
log.file.formatter.level_format = uc4
692+
```
693+
694+
The following values are valid:
695+
696+
* `lc`: full value, lower case (the default), e.g. `warning` or `info`
697+
* `uc`: full value, upper case, e.g. `WARNING` or `INFO`
698+
* `lc3`: three characters, lower case, e.g. `inf` or `dbg`
699+
* `uc3`: three characters, upper case, e.g. `INF` or `WRN`
700+
* `lc4`: four characters, lower case, e.g. `dbug` or `warn`
701+
* `uc4`: four characters, upper case, e.g. `DBUG` or `WARN`
702+
703+
### Log Message Format
704+
705+
:::warning
706+
707+
This setting should only be used as a last
708+
resort measure when overriding log format us a hard requirement
709+
of log collection tooling
710+
711+
:::
712+
713+
Besides the formatting of individual log message components
714+
(event time, log level, message, and so on), the entire log line format can be changed using
715+
the `` configuration setting.
716+
717+
The setting must be set to a message pattern that uses the following
718+
`$variables`:
719+
720+
* `$time`
721+
* `$level`
722+
* Erlang process `$pid`
723+
* Log `$msg`
724+
725+
This is what the default format looks like:
726+
727+
``` ini
728+
# '$time [$level] $pid $msg' is the default format
729+
log.console.formatter.plaintext.format = $time [$level] $pid $msg
730+
```
731+
732+
The following customized format
733+
734+
``` ini
735+
# '$time [$level] $pid $msg' is the default format
736+
log.console.formatter.plaintext.format = $level $time $msg
737+
```
738+
739+
will produce log messages that look like this:
740+
741+
```
742+
info 2024-10-04 03:23:52.968389-04:00 connection 127.0.0.1:57181 -> 127.0.0.1:5672: user 'guest' authenticated and granted access to vhost '/'
743+
debug 2024-10-04 03:24:03.338466-04:00 Will reconcile virtual host processes on all cluster members...
744+
debug 2024-10-04 03:24:03.338587-04:00 Will make sure that processes of 9 virtual hosts are running on all reachable cluster nodes
745+
```
746+
747+
Notice how the Erlang process pid is excluded. This information can be essential for
748+
root cause analysis (RCA) and therefore the default format is highly recommended.
749+
750+
751+
### JSON Field Mapping {#json-field-mapping}
752+
753+
[JSON logging](#json) can be customized in the following ways:
754+
755+
* Individual keys can be renamed by using a `{standard key}:{renamed key}` expression
756+
* Individual keys can be dropped using a `{standard key:-}` expression
757+
* All keys except for the explicitly listed ones can be dropped using a `*:-` expression
758+
759+
The `log.file.formatter.json.field_map` key then must be set
760+
to a string value that contains a number of the above expressions.
761+
762+
Before demonstrating an example, here is a message with the default mapping:
763+
764+
```json
765+
{
766+
"time":"2024-10-04 03:38:29.709578-04:00",
767+
"level":"info",
768+
"msg":"Time to start RabbitMQ: 2294 ms",
769+
"line":427,
770+
"pid":"<0.9.0>",
771+
"file":"rabbit.erl",
772+
"mfa":["rabbit","start_it",1]
773+
}
774+
775+
{
776+
"time":"2024-10-04 03:38:35.600956-04:00",
777+
"level":"info",
778+
"msg":"accepting AMQP connection 127.0.0.1:57604 -> 127.0.0.1:5672",
779+
"pid":"<0.899.0>",
780+
"domain":"rabbitmq.connection"
781+
}
782+
```
783+
784+
Now, an example that uses JSON logging with a custom field mapping:
785+
786+
```ini
787+
# log as JSON
788+
log.file.formatter = json
789+
790+
# Rename the 'time' field to 'ts', 'level' to 'lvl' and 'msg' to 'message',
791+
# drop all other fields.
792+
# Use an 'escaped string' just to make the value stand out
793+
log.file.formatter.json.field_map = 'time:ts level:lvl msg:message *:-'
794+
```
795+
796+
The example above will produce the following messages. Notice how some information
797+
is omitted compared to the default example above:
798+
799+
```json
800+
{
801+
"ts":"2024-10-04 03:34:43.600462-04:00",
802+
"lvl":"info",
803+
"message":"Time to start RabbitMQ: 2577 ms"
804+
}
805+
{
806+
"ts":"2024-10-04 03:34:49.142396-04:00",
807+
"lvl":"info",
808+
"message":"accepting AMQP connection 127.0.0.1:57507 -> 127.0.0.1:5672"
809+
}
810+
```
811+
812+
813+
### Forced Single Line Logging
814+
815+
:::warning
816+
817+
This setting can lead to incomplete log messages and should only be used as a last
818+
resort measure when overriding log format us a hard requirement
819+
of log collection tooling
820+
821+
:::
822+
823+
Multi-line messages can be truncated to a single line:
824+
825+
``` ini
826+
# Accepted values are 'on' and 'off'.
827+
# The default is 'off'.
828+
log.console.formatter.single_line = on
829+
```
830+
831+
This setting can lead to incomplete log messages and should be used only as a last
832+
resort measure.
833+
625834

626835
## Service Logs {#service-logs}
627836

@@ -644,7 +853,7 @@ The output of <code>journalctl --system</code> will look similar to this:
644853

645854
```
646855
Aug 26 11:03:04 localhost rabbitmq-server[968]: ## ##
647-
Aug 26 11:03:04 localhost rabbitmq-server[968]: ## ## RabbitMQ 3.13.7. Copyright (c) 2005-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
856+
Aug 26 11:03:04 localhost rabbitmq-server[968]: ## ## RabbitMQ 4.0.2. Copyright (c) 2005-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
648857
Aug 26 11:03:04 localhost rabbitmq-server[968]: ########## Licensed under the MPL. See https://www.rabbitmq.com/
649858
Aug 26 11:03:04 localhost rabbitmq-server[968]: ###### ##
650859
Aug 26 11:03:04 localhost rabbitmq-server[968]: ########## Logs: /var/log/rabbitmq/[email protected]

0 commit comments

Comments
 (0)