How To Manage Logfiles with Logrotate: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| Line 3: | Line 3: | ||
[[Category:201904]] | [[Category:201904]] | ||
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. This example however is limited to an independent configuration and some basic options. | <!-- retention rotate logging --> | ||
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. | |||
= Independent configuration = | |||
This example however is limited to an independent configuration and some basic options. | |||
More info here: | More info here: | ||
| Line 55: | Line 60: | ||
|} | |} | ||
= Manually logrotate = | == Manually logrotate == | ||
Option -v (verbose) is added so you can see what is happening. | Option -v (verbose) is added so you can see what is happening. | ||
| Line 63: | Line 68: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Add to daily crontab = | == Add to daily crontab == | ||
The time that was used here is daily at 4:04 am. | The time that was used here is daily at 4:04 am. | ||
| Line 72: | Line 77: | ||
04 04 * * * * /usr/sbin/logrotate /home/user1/logs/example.conf --state /home/user1/logs/logrotate-state | 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/ | |||
<syntaxhighlight lang=bash> | |||
mkdir /var/log/yourapp | |||
</syntaxhighlight> | |||
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. | |||
<syntaxhighlight lang=bash> | |||
nano /etc/logrotate.d/yourapp | |||
</syntaxhighlight> | |||
<blockquote> | |||
<syntaxhighlight lang=bash> | |||
/var/log/yourapp/*.log { | |||
daily | |||
missingok | |||
rotate 7 | |||
} | |||
</syntaxhighlight> | |||
</blockquote> | |||
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: | |||
<syntaxhighlight lang=bash> | |||
cat /var/lib/logrotate/status | |||
</syntaxhighlight> | |||
Revision as of 13:48, 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.
Independent configuration
This example however is limited to an independent configuration and some basic options.
More info here:
Complete info can be found on the man page:
man logrotate
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 containts the filenames and rotate date and time of all these logs:
cat /var/lib/logrotate/status