Log Rotation

Moath Obeidat
2 min readMay 11, 2022

What is Log Rotation?

  • It’s the process of removing or storing your old logs, without affecting your newer logs.
  • Let’s imagine that you have a server with small space, and for some reason, you forget to deal with your logs, and the system crash, it’s a nightmare.
  • So, here we need to rotate our logs, and to achieve this goal here comes the logrotate.

What is Logrotate?

  • Is a log managing command-line tool in Linux
  • Where you write your rules to manage your logs through the configuration file.
  • Logrotate helps to administer logs, compress them, remove them or even email them after a certain time period.

What are the problems the Logrotate solves?

  • Keep your server disk space managed, by storing or removing old logs.
  • Having a huge amount of logs can make things slow while you are browsing your logs or filtering for some results.
  • Having a huge amount of logs can also make things difficult to collect and ingest your logs by third-party tools.

How does it work?

Logrotate has a configuration file where we can mention all the files we want to rotate.
It needs a time period [ daily, weekly, monthly …etc ], and rotate count 3, 4, and 5 … etc for each rotation.
Log files are rotated count times before being removed.

So let’s say that the count of our rotate is “3”, that’s means rotate the log files till count 3, after logrotate start to shift the files: as logfile_1 shifted to logfile_2 and logfile_2 shifted to logfile_3 and so on. for that logrotate discard the last logfile_3 and create a new file at logfile_1.

Installation and setup?

  • Logrotate is installed by default on Ubuntu.
  • On the server, create a new file at /etc/logrotate.d/laravel and fill it with this block.
/path/to/your/laravel.log{
compress
// compress the file after rotation
daily
// rotate the log file once a day
rotate 3
// only keep the latest 3 rotations
notifempty
// if the file is empty do not rotate it
missingok
//if the file is missing, don't panic

postrotate
//implement a script doing somthing that fit your needs.
endscript
}

And there is a lot of configuration to add depending on your needs . . .

LOGROTATE

--

--