Logrotate files with date in the file name

后端 未结 11 865
孤城傲影
孤城傲影 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:28

    The current answers may work but I find they make things too complicated instead of using logrotate in a way where it already handles things very well. You don't need to specify commands manually if logrotate already handles this. You don't need complicated configuration if you just address the initial problem of catalina doing the logrotation and let logrotate handle it.


    It looks like the files are already being rotated as illustrated by the date in the logfile names in the question:

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

    So, which component already does this, is it configured in /etc/logrotate.d/tomcat*? What is the content of this file on your system currently? If I remember correctly, catalina may already doing this itself: Creating a new logfile every day and moving older files to their new location. If you want logrotate to handle this instead, you may want to change the default behaviour of tomcat / catalina, as described in this answer first.

    After that, catalina should always write to catalina.log.

    Then you can put the rotation / compression / deletion entirely into the responsibility of logrotate.

    If you want to compress older files, you can add compress to the logrotate configuration:

    For example, you have:

    /somepath/catalina.log {
      # truncate file in place
      copytruncate
      # rotate daily
      daily
      # keep max 7, will delete after that
      rotate 7
      # when rotating create file with date, e.g. catalina.log-20200304
      dateext
      # compress
      compress
      # manpage: "Postpone  compression  of  the previous log file
      #   to the next rotation cycle.  
      #   This only has effect when used in combination with compress."
      delaycompress 
      # change this to correct owner and permissions of logfile
      create 750 tomcat6 tomcat6
      # do not generate an error if logfile is missing
      missingok
      # do not rotate if the file is empty
      notifempty 
    

    }

    What you should get as a result:

    catalina.log
    catalina.log-20200304
    catalina.log-20200303.gz
    ...
    

    If things are not working:

    • execute logrotate manually with -v
    • It should also show the status file where the last rotate is logged, e.g. /var/lib/logrotate/status
    • Usually it will rotate once a day (or whatever you specified). You can force this with -f. Or you can manipulate the status file (do not do this in production unless you know what you are doing)

    A common error is that people run logrotate manually and then get puzzled if nothing is changed. Logrotate will only perform the operations it has been configured to perform. If you specify a daily rotate, it will not rotate again even if you run it as often as you want, unless you use -f. It will use the status file to keep track. You can use -v and logrotate will report what it is doing and why.

    Resources:

    • logrotate man page

提交回复
热议问题