Linux – Use Logrotate to truncate log files after every entry or clear last ‘n’ entries

bashlinuxlogginglogrotate

i have created a logrotate config file:

touch /etc/logrotate.d/.test

with the conficurations:

/home/myUser/test*.log{
    size 0
    hourly
    rotate 0    
    nocreate
    nocompress
    missingok
    nocopytruncate
    ifempty
}

I want to be able to clear the files after every entry (block log file) or in some log files to clear the last 'n' entries. Actualy, i want to keep doing:

truncate -s 0 /home/myUser/test*.log

As it is, nocopytruncate delete's the log files. If i put copytruncate it creates a second log of each log "test*.log.1", ignoring rotate 0.
If i excecute the commands bellow doesn't do anything, ignoring size 0.

echo "hello" >> ~/test1.log
cat ~/test1.log
  //output: hello

Feels like it doesn't work by using size. For this to work, I need to excecute :

logrotate -f .test

Am I doing something wrong?

Is it possible to do this by using logrotate and keep the config file hidden somehow (as it is) ?

As for the 2nd part to clear last 'n' enries, I have no idea how can i do this with logrotate.

Best Answer

Instead of using logrotate.

I'd just do something like this:

ln -s /dev/null /home/myUser/test.log

Which will cause all logging data to go to the null device.

But logging is useful and normally you shouldn't just throw it away. Most apps allow you to modify how much they log and where they log to. I'd use those methods before just throwing them away.