RabbitMQ
Log Files
File Management
System Administration
Server Maintenance

best way to rotate rabbitmq log files

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

RabbitMQ, a popular open-source message broker, uses logs to provide insights into its operations, performance issues, and debugging information. Efficient log management, including rotation, is essential to prevent logs from using excessive disk space and to maintain a clean logging environment. This article outlines the best strategies for rotating RabbitMQ log files, particularly focusing on filesystem-based log rotations and RabbitMQ's internal log rotation mechanisms.

Understanding RabbitMQ Log Files

RabbitMQ logs different types of information:

  • Erlang crash dumps: Occur when the Erlang VM crashes.
  • SASL logs: Contains information about RabbitMQ's authentication and authorization.
  • Regular logs: General logs that include client connections, disconnections, and administrative actions.

These log files are by default located in the RabbitMQ server node's base directory (e.g., /var/log/rabbitmq on Linux systems).

Log Rotation Strategies

1. Using RabbitMQ's Built-in Log Rotation

RabbitMQ supports basic log rotation natively. Configuration can be set in RabbitMQ's advanced.config file or through environment variables. Here's an example to configure log rotation in the advanced.config:

erlang
1[
2  {rabbit, [
3    {log_levels, [{connection, info}, {mirroring, info}]},
4    {log_rotation, [{file_size_limit, 10485760}, {date, "$D0"}]} % 10 MB size limit, rotate daily
5  ]}
6]

This configuration snippet does two things:

  • It sets log level details for various modules.
  • It enables log rotation when logs reach 10MB or daily.

2. External Log Rotation Tools

System administrators often use external tools like logrotate on Linux for more sophisticated log rotation mechanisms. Here is an example logrotate configuration for RabbitMQ logs:

 
1/var/log/rabbitmq/rabbit@*.log {
2    daily
3    rotate 10
4    compress
5    delaycompress
6    missingok
7    notifempty
8    create 640 rabbitmq rabbitmq
9    sharedscripts
10    postrotate
11        /usr/sbin/rabbitmqctl rotate_logs
12    endscript
13}

This configuration enables daily rotations, keeps ten backup files, compresses rotated files, and changes file permissions appropriately. The postrotate script trigger RabbitMQ to close the current log file and open a new one, ensuring that no log entries are lost during the rotation.

Best Practices for Log Rotation

  • Size-based rotation: Apart from time-based (e.g., daily), consider rotating logs once they reach a certain size to prevent any single log file from consuming too much disk space.
  • Compress old log files: Compressing older files saves significant disk space.
  • Monitor log directory size: Set up monitoring on the log directory itself to alert if total disk usage goes beyond a certain threshold.

Summary Table

MethodConfiguration LocationRotation TriggerPost-rotation Action
Built-in RabbitMQadvanced.configFile size or dateAutomatically handled
External tool (logrotate)/etc/logrotate.d/Timed or size-basedRequires manual script setup

Conclusion

Effective log management is crucial for maintaining the health and performance of RabbitMQ servers. While RabbitMQ provides some internal support for log rotation, using external tools like logrotate can provide more flexibility and robustness. By implementing these strategies, you can ensure that log files are rotated efficiently, maintaining system performance and reliability.


Course illustration
Course illustration

All Rights Reserved.