Tag: logrotate

  • logrotate nginx log by date

    Logrotate by default rotate logs with numbers like following

    logrotate nginx access log

    You can configure how many logs to keep and how to rotate lots by editing logrotate configuration file for nginx

    root@ok:~# cat /etc/logrotate.d/nginx 
    /var/log/nginx/*.log {
    	daily
    	missingok
    	rotate 14
    	compress
    	delaycompress
    	notifempty
    	create 0640 www-data adm
    	sharedscripts
    	prerotate
    		if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
    			run-parts /etc/logrotate.d/httpd-prerotate; \
    		fi \
    	endscript
    	postrotate
    		invoke-rc.d nginx rotate >/dev/null 2>&1
    	endscript
    }
    root@ok:~# 

    rotate 14 – this tells logrotate to keep logs for 14 days.

    If you need to rotate logs by date, add

            dateext
            dateformat -%Y-%m-%d

    Example

    /var/log/nginx/*.log {
            daily
            missingok
            rotate 14
            compress
            delaycompress
            notifempty
            create 0640 www-data adm
            dateext
            dateformat -%Y-%m-%d
            sharedscripts
            prerotate
                    if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                            run-parts /etc/logrotate.d/httpd-prerotate; \
                    fi \
            endscript
            postrotate
                    invoke-rc.d nginx rotate >/dev/null 2>&1
            endscript
    }

    See Logrotate

  • logrotate

    logrotate is a program to rotates logs. This is run daily using cronjob

    /etc/cron.daily/logrotate
    

    Logrotate config files are stored in

    /etc/logrotate.d/
    

    To rotate logs daily, add daily to the logrotate configuration. To keep logs for 5 days, add “rotate 5”.

    Rotating a log

    To rotate a log

    /var/log/sync-servers.log
    

    Create file

    vi /etc/logrotate.d/sync-servers
    

    with content

    /var/log/sync-servers.log
    {
    	create 640 root adm
    	missingok
    	notifempty
    	rotate 4
    	weekly
    }
    

    logrotate nginx log by date