Advice wanted: Log rotation without systemctl?

I’m wondering if using logrotate is the best approach for rotating the logs written by our various programs in a container. All logs are written to a persistent shared volume.

We have in one container:

  • a C++ program that writes to a log file,
  • a bash script that is our dockerfile CMD and it writes to a log file,
  • a health check as bash script that also writes to a log file

In another container:

  • a tomcat host java servlet that writes logs using log4j2
  • a bash script that is our dockerfile CMD and it writes to a log file,
  • a C++ program that writes to a log file

In another container:

  • an nginx proxy that writes to access and error logs

Other than the log4j2 logging, which has the ability to rotate logs, all of the logs are being rotated using logrotate, which is if I understand it correctly requires systemctl. Which may require INITSYSTEM=0.

Is there a better way to rotate logs than using logrotate in each container? I realize that having a single container rotate all of the logs would require a single list of logs to be configured, although we could use a file pattern to rotate everything the same way.

Hi @jason10,

While logrotate is often controlled via systemd timer, it is not strictly necessary. You can call logrotate from any daemonized program if you like. It is quite common to run such housekeeping tasks from cron if systemd is not available on device.

Personally, I would recommend logging from all containers to a single root (ie mapping each container’s storage in a central place, like /data/logs/{container1,container2,container3} and then rotating those files from another container altogether, using a logrotate configuration similar to the following (ymmv). You should be able to run this all from a simple crontab entry or daemon.

/data/logs/*/* {
    monthly
    rotate 2
    olddir /data/logs/old
    missingok
}

Please do let us know what you decide to do!

I’m going to go with writing all of the logs to a shared volume and have one of the containers that already requires systemctl specify the log rotation. Thanks