Beginners guide to logrotate.
Tags: nginx, logrotate, ubuntu, beginners
Published: 2019-04-21 12:19:01

---
title: Beginners guide to logrotate.
published: true
description: Managing large log files generate by system process
tags: nginx, logrotate, ubuntu, beginner
---

Now that you have [installed](https://dev.to/rshiva/welcome-to-nginx-part-i-58mn) and [setup](https://dev.to/rshiva/welcome-to-nginx-part-ii-32km) Nginx in production for your web application. Your site is getting a good amount of traffic and all the logs are written in single file. This can be cumbersome to perform a certain task on file like searching and archiving the logs. Wouldn't be easy if can write daily logs on a separate file and compress the old logs and archive it separate place this will help us in optimizing the hard disk space.

## Welcome to logrotate. ##

Logrotate is built-in ubuntu. With Logrotate we can do automatic rotation, compression, deletion and mailing the files. Each file can be rotated daily, weekly or monthly basis or when the file size becomes too large. 


Main logrotate configuration is in __/etc/logrotate.conf__ .If you look into that file which has configuration many default configuration. Before we dive into the configuration you should see below line in the file.

    include /etc/logrotate.d/


 Rather than writing all the process log into single file, let's create a separate configuration file so that it will be easy to maintain.First, we need to create an nginx conf file inside __/etc/logrotate.d/__

    cd /etc/logrotate.d/
    sudo touch nginx

Let's write the below basic configuration into that file and save it. I will run you through what each line does.

    /etc/nginx/log/*.log {
      daily
      rotate 15
      missingok
      compress
      delaycompress
      notifempty
      dateext
      dateformat .%Y-%m-%d
      create 664 www-data www-data
      sharedscripts
      postrotate
  	    [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
      endscript
    }


__/etc/nginx/log/*.log__ : Is the path of logs file you want to rotate.
__daily__ :  Rotate on a daily basis.
__size__ : (100K/100M/100G) This will logrotate the log whenever the size of the file increases as configured.
__rotate 15__: Only last 15 log file are kept and others will be deleted. If rotate count is 0 old versions are deleted rather than rotated
__missingok__ : If the log file is missing move to next file without throwing error.
__nomissingok__ : If log file doesn't exist throw an error.
__compress__ : It compresses the rotated file by default it uses gzip and file formats ending with .gz.
__delaycompress__: Delaycompress as the name suggest it delays compression of the log file so that previous processes after a restart or reload will be still written in the old log file so that we don't miss any logs. This will only work if you have __compress__ in the configuration.
__notifempty__ : Do not rotate the file if it's empty.
__dateext__ : Archiving the old log file with date extension rather than by number. Format for a daily extension is YYYYMMDD. This is really handy when you need to search anything on a specific date.
__create mode owner group__: This creates a new log file with mode, owner, and the group immediately after rotation.
__dateformat__ : To specify the date format extension to dateext.
__sharedscripts__ : Sharedscripts are used with postrotate or prerotate, any scripts added to the postscript to run only once. 
__postrotate__ :  Script inside the postrotate and endscript are executed after the logfile is rotated. This script does not kill the nginx process instead it sends a signal to reload the log files so that new requests are logged in the new file.
__prerotate__ : Script inside this block will be executed before the logrotate.

This tutorial is just to get you started with logrotate. There are many configurations in logrotate like mailing the logs to the specific email address and many more. I encourage to check the inbuild documentation(man logratote)