How To Manage Logfiles with Logrotate: Difference between revisions
| (2 intermediate revisions by the same user not shown) | |||
| Line 103: | Line 103: | ||
Start-off with placing your app-logs in your own app directory under /var/log/ | Start-off with placing your app-logs in your own app directory under /var/log/ | ||
< | <source lang=bash> | ||
mkdir /var/log/yourapp | sudo mkdir /var/log/yourapp | ||
</ | </source> | ||
A logfile could be '''/var/log/yourapp/yourapp.log''' | A logfile could be '''/var/log/yourapp/yourapp.log''' | ||
| Line 111: | Line 111: | ||
Sytem logrotate default settings can be found in the file '''/etc/logrotate.conf''' you don't have to edit this file. 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 in a new file. | Sytem logrotate default settings can be found in the file '''/etc/logrotate.conf''' you don't have to edit this file. 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 in a new file. | ||
< | <source lang=bash> | ||
nano /etc/logrotate.d/yourapp | sudo nano /etc/logrotate.d/yourapp | ||
</ | </source> | ||
<blockquote> | <blockquote> | ||
< | <source lang=bash> | ||
/var/log/yourapp/*.log { | /var/log/yourapp/*.log { | ||
rotate 4 | |||
daily | daily | ||
size 5k | |||
missingok | missingok | ||
create | |||
compress | |||
notifempty | |||
delaycompress | |||
} | } | ||
</ | </source> | ||
</blockquote> | </blockquote> | ||
<source lang=bash> | |||
sudo chmod u=rw,g=r,o=r /etc/logrotate.d/yourapp | |||
</source> | |||
Rotating will not be done more that once a day as it will be part of a daily crontab job. The log status for all the logs (name,date and time) will be kept in a single file: | Rotating will not be done more that once a day as it will be part of a daily crontab job. The log status for all the logs (name,date and time) will be kept in a single file: | ||
< | <source lang=bash> | ||
cat /var/lib/logrotate/status | cat /var/lib/logrotate/status | ||
</ | </source> | ||
Trigger logrotate manually: | |||
<source lang=bash> | |||
sudo logrotate /etc/logrotate.d/ | |||
</source> | |||
= Empty a logfile as admin = | = Empty a logfile as admin = | ||
Latest revision as of 13:56, 11 April 2021
See also: Systemd logging
Systemd logging aims to be better than syslog. Usually both Systemd and Syslog are available on the system. You might consider looking into it:
Sent log message from shell script:
#-p [severity 0-7] 0=emergency, 2=error, 6=info, 7=debug echo "[wjv backup] backup error message" | systemd-cat -t "identifier" -p 2
Syslog
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 10k
create
compress
delaycompress
}
This file will be ignored if it hasn't the correct permissions. Permissions that work are 'all=read' (444) OR 'user/owner=read/write, group and other=read' (644):
chmod u=rw,g=r,o=r /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) |
| notifempty | Do not rotate if the logfile is empty |
| create | Create new empty logfile after rotation |
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
Linux allows you to use the general logging and logrotate process. It will help if you use the standard files and locations to reduce the complexity of your app.
Start-off with placing your app-logs in your own app directory under /var/log/
sudo 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 you don't have to edit this file. 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 in a new file.
sudo nano /etc/logrotate.d/yourapp
/var/log/yourapp/*.log {
rotate 4
daily
size 5k
missingok
create
compress
notifempty
delaycompress
}
sudo chmod u=rw,g=r,o=r /etc/logrotate.d/yourapp
Rotating will not be done more that once a day as it will be part of a daily crontab job. The log status for all the logs (name,date and time) will be kept in a single file:
cat /var/lib/logrotate/status
Trigger logrotate manually:
sudo logrotate /etc/logrotate.d/
Empty a logfile as admin
Clear:
echo "" | sudo tee /var/log/logfile.log cat /var/log/logfile.log
Append (-a):
echo "append this line" | sudo tee -a /var/log/logfile.log