Log rotation

Visakh Vijayan
2 min readMar 2, 2021

--

We all love to keep logs of our work. Seldom do we use it afterward, but it’s always good to keep them to resolve disputes between teams. For e.g. in our team, we use logs to keep track of API requests sent by the app to the backend.

If you are using a Linux machine, it’s too simple. All you have to do is use the logrotate command. Here is how -

  1. Navigate to the /etc/logrotate.d/ folder. You will find several different files there.
  2. Create a new file say “my_log_file” using touch my_log_file command.
  3. Add the below to your file. Use sudo nano my_log_file to open.
<path-to-your-log-file> {
daily
maxsize 50M
rotate 10
compress
create 744 root root
missingok
}

4. There are many different options online.

5. Here is a summary of the above options:

daily - (when the log is to be rotated),
maxsize - (when the file should be rotated. Here it’s either daily or once the file reaches 50 MB, whichever is earlier),
rotate - (how many rotated files will be stored. By default, it rotates a file and appends .1 to it. So if your file is abc, it will rotate and create abc.1, abc.2, and so on.
compress - the log file can be compressed. This can be observed as the file will have a .gz appended to its name once rotated.
create - by default after rotating, a new file is not created. You need this option if you want to create a new file with the name abc back.
missingok - tell logrotate command to not throw an error when a log file is not found.

6. There are lots of other options. You can even mail the log files if you don’t want them deleted after <n> rotations.

7. To check if things are working properly you can run this

logrotate -f my_log_file

This will create a rotation. You can also see the visit the status file inside /var/lib/logrotate/status. This keeps a track of when the log files were rotated.

Happy logging!

--

--

Visakh Vijayan
Visakh Vijayan

Written by Visakh Vijayan

Techie from Kerala, India. Days are for coding, nights for weaving tales of tech, travel, and finance. Join me in exploring this multifaceted journey

No responses yet