How To Manage Logfiles with Logrotate: Difference between revisions

From WickyWiki
Line 86: Line 86:
</syntaxhighlight>
</syntaxhighlight>


A logfile could be /var/log/yourapp/yourapp.log
A logfile could be '''/var/log/yourapp/yourapp.log'''


Sytem logrotate default settings can be found in the file /etc/logrotate.conf
Sytem logrotate default settings can be found in the file '''/etc/logrotate.conf'''


The file /etc/logrotate.conf will include all configuration files in the directory /etc/logrotate.d/. You should place 'yourapp' logrotate configuration in that directory.
The file '''/etc/logrotate.conf''' will include all configuration files in the directory '''/etc/logrotate.d/'''. You should place 'yourapp' logrotate configuration in that directory.


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Line 108: Line 108:
Rotating will not be done more that once a day as it is part of a daily crontab job by default
Rotating will not be done more that once a day as it is part of a daily crontab job by default


System default log status file containts the filenames and rotate date and time of all these logs:
System default log status file contains the filenames and rotate date and time of all these logs:


<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
cat /var/lib/logrotate/status
cat /var/lib/logrotate/status
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:33, 6 March 2019


To prevent your log files from growing without limit the 'logrotate' command can be used to specify what you want to keep. There are a lot of options and you might want to integrate your log-cleaning into the daily logrotate schedule.

More info here:

Complete info can be found on the man page:

man logrotate

Independent configuration

This example is limited to an independent configuration and some basic options.

Create logrotate config file:

sudo nano /home/user1/logs/example.conf
/home/user1/logs/*.log {
    rotate 4
    daily
    size 1k
    missingok
    compress
    delaycompress
}
chmod 0444 /home/user1/logs/example.conf

Options explained:

Option Meaning
rotate 4 Keep 4 archived logfiles (1-4), older files are disposed of
daily Rotate not more than once a day, the state-file is used to check if the logfile was rotated earlier today.
size 1k Do not rotate if the logfile size is less than 1k
missingok Do not report an error if the logfile does not exist
compress Compress (gz) rotated logfiles
delaycompress Compress the last archived logfile with the next rotation (2-4 will be compressed)

Manually logrotate

Option -v (verbose) is added so you can see what is happening.

logrotate -v /home/user1/logs/example.conf --state /home/user1/logs/logrotate-state

Add to daily crontab

The time that was used here is daily at 4:04 am.

nano /etc/crontab
 04 04 * * * *  /usr/sbin/logrotate /home/user1/logs/example.conf --state /home/user1/logs/logrotate-state

System settings and locations for logrotate

Place your logs in your own app directory under /var/log/

mkdir /var/log/yourapp

A logfile could be /var/log/yourapp/yourapp.log

Sytem logrotate default settings can be found in the file /etc/logrotate.conf

The file /etc/logrotate.conf will include all configuration files in the directory /etc/logrotate.d/. You should place 'yourapp' logrotate configuration in that directory.

nano /etc/logrotate.d/yourapp
/var/log/yourapp/*.log {
    daily
    missingok
    rotate 7
}

Rotating will not be done more that once a day as it is part of a daily crontab job by default

System default log status file contains the filenames and rotate date and time of all these logs:

cat /var/lib/logrotate/status