Logrotate files with date in the file name

后端 未结 11 868
孤城傲影
孤城傲影 2020-12-13 05:08

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

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 05:23

    To include a date in the rotated file, you can probably use 'dateext' option.

    $ cat logrotate.conf 
    /var/nginx/logs/access.log {
        size 10k
        copytruncate
        dateext
        rotate 10
        compress
    }
    

    The rotated file should get created similar to below

     root@nitpc:~# ls -lrt /var/nginx/logs/access.*
    -rw-r--r-- 1 nginx root 5422 May 31 08:26 access.log
    -rw-r--r-- 1 nginx root  466 May 31 08:26 access.log-20180531.gz
    

    The only downside is you won't be able to run it more than once per day as the file would have a definite name for that date.

    The above example is from my Nginx docker container running in k8s on GC. The logrotate version is 3.11.0.

    Hope that helps!

    Update: From man pages https://linux.die.net/man/8/logrotate

    dateformat format string

    Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.

提交回复
热议问题