<commit_before># Logging

## <a id="overview" class="anchor" href="#overview">Overview</a>

Log files is a very important aspect of system observability,
much like [monitoring](monitoring.html).

Developers and operators should inspect logs when troubleshooting an issue or assessing the
state of the system.

RabbitMQ supports a number of features when it comes to logging.

This guide covers topics such as:

 * Supported log outputs: [file](#logging-to-a-file) and [standard streams (console)](#logging-to-console)
 * [Log file location](#log-file-location)
 * Supported [log levels](#log-levels)
 * How to [enable debug logging](#debug-logging)
 * How to [tail logs of a running node](#log-tail) without having access to the log file
 * Watching [internal events](#internal-events)
 * [Connection lifecycle events](#logged-events) logged
 * [Logging in JSON](#json)
 * [Log categories](#log-message-categories)
 * How to [inspect service logs](#service-logs) on systemd-based Linux systems
 * [Log rotation](#log-rotation)
 * [Logging to Syslog](#logging-to-syslog)
 * [Logging to a system topic exchange](#log-exchange), `amq.rabbitmq.log`

and more.

## <a id="log-file-location" class="anchor" href="#log-file-location">Log File Location</a>

Modern RabbitMQ versions use a single log file by default.

Please see the [File and Directory Location](relocate.html) guide to find default log file location for various platforms.

There are two ways to configure log file location. One is the [configuration file](configure.html). This option is recommended.
The other is the `RABBITMQ_LOGS` environment variable. It can be useful in development environments.

Use [RabbitMQ management UI](management.html) or [`rabbitmq-diagnostics -q log_location`](cli.html)
to find when a node stores its log file(s).

The `RABBITMQ_LOGS` variable value can be either a file path or a hyphen (`-`).
`RABBITMQ_LOGS=-` will result in all log messages being sent to standard output.
See [Logging to Console (Standard Output)](#logging-to-console).

The environment variable takes precedence over the configuration file. When in doubt, consider
overriding log file location via the config file. As a consequence of the environment variable precedence,
if the environment variable is set, the configuration key `log.file` will not have any effect.


## <a id="configuration" class="anchor" href="#configuration">Configuration</a>

RabbitMQ starts logging early on node start. See the [Configuration guide](configure.html)
for a general overview of how to configure RabbitMQ.

### <a id="log-outputs" class="anchor" href="#log-outputs">Log Outputs</a>

Default RabbitMQ logging configuration will direct log messages to a log file. Standard output is
another option available out of the box.

Several outputs can be used at the same time. Log entries will be copied to all of them.

Different outputs can have different log levels. For example, the console output can log all
messages including debug information  while the file output can only log error and higher severity
messages.


### <a id="logging-to-a-file" class="anchor" href="#logging-to-a-file">Logging to a File</a>

 * `log.file`: log file path or `false` to disable the file output. Default value is taken from the `RABBITMQ_LOGS` [environment variable or configuration file](configure.html)
 * `log.file.level`: log level for the file output. Default level is `info`
 * `log.file.formatter`: controls log entry format, text lines or JSON
 * `log.file.rotation.date`, `log.file.rotation.size`, `log.file.rotation.count` for log file rotation settings
 * `log.file.formatter.time_format`: controls timestamp formatting

The following example overrides log file name:

<pre class="lang-ini">
log.file = rabbit.log
</pre>

The following example overrides log file directory:

<pre class="lang-ini">
log.dir = /data/logs/rabbitmq
</pre>

The following example instructs RabbitMQ to log to a file at the `debug` level:

<pre class="lang-ini">
log.file.level = debug
</pre>

Supported log levels ca be found in the [example rabbitmq.conf file](https://github.com/rabbitmq/rabbitmq-server/blob/v3.9.x/deps/rabbit/docs/rabbitmq.conf.example).

Logging to a file can be disabled with

<pre class="lang-ini">
log.file = false
</pre>

Logging in JSON format to a file:

<pre class="lang-ini">
log.file.formatter = json
</pre>

By default, RabbitMQ will use RFC 3339 timestamp format. It is possible
to switch to a UNIX epoch-based format:

<pre class="lang-ini">
log.file = true
log.file.level = info

# use microseconds since UNIX epoch for timestamp format
log.file.formatter.time_format = epoch_usecs
</pre>

The rest of this guide describes more options, including [more advanced ones](#advanced-configuration).

### <a id="log-rotation" class="anchor" href="#log-rotation">Log Rotation</a>

RabbitMQ nodes always append to the log files, so a complete log history is preserved.
Log file rotation is not performed by default. [Debian](install-debian.html) and [RPM](install-rpm.html) packages will set up
log [rotation via `logrotate`](#logrotate) after package installation.

`log.file.rotation.date`, `log.file.rotation.size`, `log.file.rotation.count` settings control log file rotation
for the file output.

#### Built-in Periodic Rotation

Use `log.file.rotation.date` to set up minimalistic periodic rotation:

<pre class="lang-ini">
# rotate every night at midnight
log.file.rotation.date = $D0

# keep up to 5 archived log files in addition to the current one
log.file.rotation.count = 5
</pre>

<pre class="lang-ini">
# rotate every day at 23:00 (11:00 p.m.)
log.file.rotation.date = $D23
</pre>

<pre class="lang-ini">
# rotate every night at midnight
log.file.rotation.date = $D0
</pre>

#### Built-in File Size-based Rotation

`log.file.rotation.size` controls rotation based on the current log file size:

<pre class="lang-ini">
# rotate when the file reaches 10 MiB
log.file.rotation.size = 10485760

# keep up to 5 archived log files in addition to the current one
log.file.rotation.count = 5
</pre>

#### <a id="logrotate" class="anchor" href="#logrotate">Rotation Using Logrotate</a>

On Linux, BSD and other UNIX-like systems, [logrotate](https://linux.die.net/man/8/logrotate) is an alternative
way of log file rotation and compression.

RabbitMQ [Debian](./install-debian.html) and [RPM](./install-rpm.html) packages will set up `logrotate` to run weekly on files
located in default `/var/log/rabbitmq` directory. Rotation configuration can be found in `/etc/logrotate.d/rabbitmq-server`.


### <a id="logging-to-console" class="anchor" href="#logging-to-console">Logging to Console (Standard Output)</a>

Here are the main settings that control console (standard output) logging:

 * `log.console` (boolean): set to `true` to enable console output. Default is `false
 * `log.console.level`: log level for the console output. Default level is `info`
 * `log.console.formatter`: controls log entry format, text lines or JSON
 * `log.console.formatter.time_format`: controls timestamp formatting

To enable console logging, use the following config snippet:

<pre class="lang-ini">
log.console = true
</pre>

The following example disables console logging

<pre class="lang-ini">
log.console = false
</pre>

The following example instructs RabbitMQ to use the `debug` logging level when logging to console:
<commit_msg>Clarify some docs about log outputs<commit_after># Logging

## <a id="overview" class="anchor" href="#overview">Overview</a>

Log files is a very important aspect of system observability,
much like [monitoring](monitoring.html).

Developers and operators should inspect logs when troubleshooting an issue or assessing the
state of the system.

RabbitMQ supports a number of features when it comes to logging.

This guide covers topics such as:

 * Supported [log outputs](#log-outputs): [file](#logging-to-a-file) and [standard streams (console)](#logging-to-console)
 * [Log file location](#log-file-location)
 * Supported [log levels](#log-levels)
 * How to [enable debug logging](#debug-logging)
 * How to [tail logs of a running node](#log-tail) without having access to the log file
 * Watching [internal events](#internal-events)
 * [Connection lifecycle events](#logged-events) logged
 * [Logging in JSON](#json)
 * [Log categories](#log-message-categories)
 * How to [inspect service logs](#service-logs) on systemd-based Linux systems
 * [Log rotation](#log-rotation)
 * [Logging to Syslog](#logging-to-syslog)
 * [Logging to a system topic exchange](#log-exchange), `amq.rabbitmq.log`

and more.

## <a id="log-outputs" class="anchor" href="#log-outputs">Log Outputs</a>

[Logging to a file](#logging-to-a-file) is one of the most common options for RabbitMQ installations.
[Logging to standard output and error streams](#logging-to-console) is another popular option. [Syslog](#logging-to-syslog)
is another option supported out of the box.

Different outputs can have different log levels. For example, the console output can log all
messages including debug information  while the file output can only log error and higher severity
messages.

## <a id="log-file-location" class="anchor" href="#log-file-location">Log File Location</a>

Please see the [File and Directory Location](relocate.html) guide to find default log file location for various platforms.

There are two ways to configure log file location. One is the [configuration file](configure.html). This option is recommended.
The other is the `RABBITMQ_LOGS` environment variable. It can be useful in development environments.

Use [RabbitMQ management UI](management.html) or [`rabbitmq-diagnostics -q log_location`](cli.html)
to find when a node stores its log file(s).

The `RABBITMQ_LOGS` variable value can be either a file path or a hyphen (`-`).
`RABBITMQ_LOGS=-` will result in all log messages being sent to standard output.
See [Logging to Console (Standard Output)](#logging-to-console).

The environment variable takes precedence over the configuration file. When in doubt, consider
overriding log file location via the config file. As a consequence of the environment variable precedence,
if the environment variable is set, the configuration key `log.file` will not have any effect.


## <a id="configuration" class="anchor" href="#configuration">Configuration</a>

RabbitMQ starts logging early on node start. See the [Configuration guide](configure.html)
for a general overview of how to configure RabbitMQ.

### <a id="logging-to-a-file" class="anchor" href="#logging-to-a-file">Logging to a File</a>

Logging to a file is one of the most common options for RabbitMQ installations. In modern releases,
RabbitMQ nodes only log to a file if explicitly configured to do so using
the configuration keys listed below:

 * `log.file`: log file path or `false` to disable the file output. Default value is taken from the `RABBITMQ_LOGS` [environment variable or configuration file](configure.html)
 * `log.file.level`: log level for the file output. Default level is `info`
 * `log.file.formatter`: controls log entry format, text lines or JSON
 * `log.file.rotation.date`, `log.file.rotation.size`, `log.file.rotation.count` for log file rotation settings
 * `log.file.formatter.time_format`: controls timestamp formatting

The following example overrides log file name:

<pre class="lang-ini">
log.file = rabbit.log
</pre>

The following example overrides log file directory:

<pre class="lang-ini">
log.dir = /data/logs/rabbitmq
</pre>

The following example instructs RabbitMQ to log to a file at the `debug` level:

<pre class="lang-ini">
log.file.level = debug
</pre>

Supported log levels ca be found in the [example rabbitmq.conf file](https://github.com/rabbitmq/rabbitmq-server/blob/v3.9.x/deps/rabbit/docs/rabbitmq.conf.example).

Logging to a file can be disabled with

<pre class="lang-ini">
log.file = false
</pre>

Logging in JSON format to a file:

<pre class="lang-ini">
log.file.formatter = json
</pre>

By default, RabbitMQ will use RFC 3339 timestamp format. It is possible
to switch to a UNIX epoch-based format:

<pre class="lang-ini">
log.file = true
log.file.level = info

# use microseconds since UNIX epoch for timestamp format
log.file.formatter.time_format = epoch_usecs
</pre>

The rest of this guide describes more options, including [more advanced ones](#advanced-configuration).

### <a id="log-rotation" class="anchor" href="#log-rotation">Log Rotation</a>

RabbitMQ nodes always append to the log files, so a complete log history is preserved.
Log file rotation is not performed by default. [Debian](install-debian.html) and [RPM](install-rpm.html) packages will set up
log [rotation via `logrotate`](#logrotate) after package installation.

`log.file.rotation.date`, `log.file.rotation.size`, `log.file.rotation.count` settings control log file rotation
for the file output.

#### Built-in Periodic Rotation

Use `log.file.rotation.date` to set up minimalistic periodic rotation:

<pre class="lang-ini">
# rotate every night at midnight
log.file.rotation.date = $D0

# keep up to 5 archived log files in addition to the current one
log.file.rotation.count = 5
</pre>

<pre class="lang-ini">
# rotate every day at 23:00 (11:00 p.m.)
log.file.rotation.date = $D23
</pre>

<pre class="lang-ini">
# rotate every night at midnight
log.file.rotation.date = $D0
</pre>

#### Built-in File Size-based Rotation

`log.file.rotation.size` controls rotation based on the current log file size:

<pre class="lang-ini">
# rotate when the file reaches 10 MiB
log.file.rotation.size = 10485760

# keep up to 5 archived log files in addition to the current one
log.file.rotation.count = 5
</pre>

#### <a id="logrotate" class="anchor" href="#logrotate">Rotation Using Logrotate</a>

On Linux, BSD and other UNIX-like systems, [logrotate](https://linux.die.net/man/8/logrotate) is an alternative
way of log file rotation and compression.

RabbitMQ [Debian](./install-debian.html) and [RPM](./install-rpm.html) packages will set up `logrotate` to run weekly on files
located in default `/var/log/rabbitmq` directory. Rotation configuration can be found in `/etc/logrotate.d/rabbitmq-server`.


### <a id="logging-to-console" class="anchor" href="#logging-to-console">Logging to Console (Standard Output)</a>

Logging to standard streams (console) is another popular option for RabbitMQ installations.
RabbitMQ nodes only log to standard streams if explicitly configured to do so.

Here are the main settings that control console (standard output) logging:

 * `log.console` (boolean): set to `true` to enable console output. Default is `false
 * `log.console.level`: log level for the console output. Default level is `info`
 * `log.console.formatter`: controls log entry format, text lines or JSON
 * `log.console.formatter.time_format`: controls timestamp formatting

To enable console logging, use the following config snippet:

<pre class="lang-ini">
log.console = true
</pre>

The following example disables console logging

<pre class="lang-ini">
log.console = false
</pre>

The following example instructs RabbitMQ to use the `debug` logging level when logging to console:

{'fraction_non_alphanumeric': 0.1073807796319504, 'fraction_numerical': 0.004353274849943935, 'mean_word_length': 4.860842674913027, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1149848', 'n_tokens_mistral': 4521, 'n_tokens_neox': 4245, 'n_words': 1782}