How To Manage Logfiles with Logrotate
See also: Systemd
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/
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.
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 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
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