How to Rotate Log Files in Fluentd?

Better Stack Team
Updated on February 5, 2024

Fluentd offers the capability to set up log rotation, particularly useful when managing large volumes of logs that consume significant disk space. This feature can be configured through Fluentd's system-wide settings.

Here is an example of a log rotation configuration in Fluentd that can rotate logs daily:

/etc/fluent/fluentd.conf
<system>
  <log>
    rotate_age 7
    rotate_size 1048576
  </log>
</system>

The configuration options are as follows:

  • rotate_age: This parameter specifies the maximum age of log files in days before they are rotated. In this example, logs older than seven days will be rotated.

  • rotate_size: This option defines the maximum file size in bytes for a log file before it gets rotated. Here, the file size threshold for rotation is set at 1MB.

Additionally, the rotate_age option can accept string values for more dynamic scheduling:

/etc/fluent/fluentd.conf
<system>
  <log>
    rotate_age weekly
    rotate_size 1048576
  </log>
</system>

Setting rotate_age to "weekly" means that the files will be rotated every week.

These settings are not limited to configuration files. You can also pass them as command-line arguments:

 
fluentd -c fluent.conf --log-rotate-age 7 --log-rotate-size 104857600

🔭 Want to centralize and monitor your logs?

Go to Logtail and start your log management in 5 minutes.

Better Uptime Dashboard