Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

tomcat - Logrotate files with date in the file name

I am trying to configure logrotate in RHEL for tomcat6 logs. Currently, logrotate works fine for catalina.out log, it is rotated and compressed properly.

The problem is with the files with date in them like:

catalina.2012-01-20.log
catalina.2012-01-21.log
catalina.2012-01-22.log

These files are not being rotated. I understand that I have to configure these in /etc/logrotate.d/tomcat6 file where rotation for catalina.out is configured. But I am not able to configure it.

All I want is these older files to be compressed daily, except the current date log file.

Can anybody help me out on this, please!!

Thanks Noman A.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(First post ever so if it looks like a drunk spider has formatted it then sorry)

After using our friend Google, here and I can't remember where else I managed to achieve something using logrotate (rather than cron or some other equivalent).

I have a the following in /var/log/rsync/:

-rw-r--r-- 1 root root 1.1M Apr  9 08:13 2014-04-09 07:48:18.log
-rw-r--r-- 1 root root 1.4M Apr 11 15:20 2014-04-11 15:02:52.log
-rw-r--r-- 1 root root 1.6M Apr 11 15:42 2014-04-11 15:22:04.log
-rw-r--r-- 1 root root 1.8M Apr 12 08:01 2014-04-12 07:45:31.log
-rw-r--r-- 1 root root 2.0M Apr 13 08:10 2014-04-13 07:53:38.log
-rw-r--r-- 1 root root 2.2M Apr 14 08:19 2014-04-14 07:51:09.log
-rw-r--r-- 1 root root 2.5M Apr 15 08:05 2014-04-15 07:37:38.log
-rw-r--r-- 1 root root 2.7M Apr 16 08:11 2014-04-16 07:43:14.log

and the following logrotate file:

/var/log/rsync/*.log {
       daily
       rotate 7
       compress
       delaycompress
       notifempty
       missingok
}

which I thought was perfectly reasonable. But after it refused to work and on finding out that it would never work (courtesy of this post) I wondered if it could be fudged to make it work.

After much testing and tweaking I managed to fudge it the following way:

/var/log/rsync/dummy {
        daily
        rotate 0
        create
        ifempty
        lastaction
                /usr/bin/find /var/log/rsync/ -mtime +7 -delete
                /usr/bin/find /var/log/rsync/ -mtime +1 -exec gzip -q {} ;
        endscript
}

into a logrotate config file called /etc/logrotate.d/local-rsync. Then create the dummy log file:

touch /var/log/rsync/dummy

then force a logrotate with:

logrotate -fv /etc/logrotate.d/local-rsync

which gives:

-rw-r--r-- 1 root root  71K Apr  9 08:13 2014-04-09 07:48:18.log.gz
-rw-r--r-- 1 root root  88K Apr 11 15:20 2014-04-11 15:02:52.log.gz
-rw-r--r-- 1 root root  82K Apr 11 15:42 2014-04-11 15:22:04.log.gz
-rw-r--r-- 1 root root  84K Apr 12 08:01 2014-04-12 07:45:31.log.gz
-rw-r--r-- 1 root root  87K Apr 13 08:10 2014-04-13 07:53:38.log.gz
-rw-r--r-- 1 root root  92K Apr 14 08:19 2014-04-14 07:51:09.log.gz
-rw-r--r-- 1 root root 2.5M Apr 15 08:05 2014-04-15 07:37:38.log
-rw-r--r-- 1 root root 2.7M Apr 16 08:11 2014-04-16 07:43:14.log
-rw-r--r-- 1 root root    0 Apr 16 12:11 dummy

Now just wait for tomorrow morning...

I realise that cron would be tidier however I have another element in the logrotate config file and wanted to keep the two together.

Bonus with the dummy file is that it doesn't take up any space!

You may find that it does not appear to have rotated anything one day. It took me while to work out why but then it twigged. find -mtime +1 is whole days (i.e. 24*60 minutes) and if the daily logrotate kicked in less than 24 hours since the last time/time the logs were created then it sometimes appears not to have worked. If it bothers you then using 23 hours with find -mmin +1380 might be more appropriate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...