Logrotate 文件名中包含日期的文件

发布于 2024-12-28 03:15:13 字数 384 浏览 2 评论 0原文

我正在尝试在 RHEL 中为 tomcat6 日志配置 logrotate。目前,logrotate 对于 catalina.out 日志工作得很好,它被正确地旋转和压缩。

问题在于其中包含日期的文件,例如:

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

这些文件没有被轮换。我知道我必须在 /etc/logrotate.d/tomcat6 文件中配置这些,其中配置了 catalina.out 的轮换。但我无法配置它。

我想要的只是每天压缩这些旧文件,除了当前日期日志文件。

有人可以帮我解决这个问题吗?

谢谢 诺曼 A.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

全部不再 2025-01-04 03:15:13

(有史以来第一篇文章,如果它看起来像喝醉的蜘蛛已经格式化了,那么抱歉)

在使用我们的朋友谷歌之后,在这里,我不记得我还能在哪里使用 logrotate (而不是 cron 或其他类似的东西)来实现一些东西。

中有以下内容

-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

我在 /var/log/rsync/:和以下 logrotate 文件

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

:我认为这是完全合理的。但在它拒绝工作并发现它永远不会工作(由这篇文章提供)之后,我想知道是否可以捏造它使其工作。

经过大量测试和调整后,我设法通过以下方式捏造它:

/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
}

进入名为 /etc/logrotate.d/local-rsync 的 logrotate 配置文件。然后创建虚拟日志文件:

touch /var/log/rsync/dummy

然后强制执行 logrotate:

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

给出:

-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

现在只需等待明天早上...

我意识到 cron 会更整洁,但是我在 logrotate 配置文件中有另一个元素,并且希望将两者放在一起。

虚拟文件的好处是它不占用任何空间!

您可能会发现有一天它似乎没有旋转任何东西。我花了一段时间才弄清楚原因,但后来它发生了变化。 find -mtime +1 是一整天(即 24*60 分钟),如果自上次创建日志后不到 24 小时内开始每日日志轮转,那么有时似乎不起作用。如果它困扰您,那么使用 23 小时和 find -mmin +1380 可能更合适。

(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.

月棠 2025-01-04 03:15:13

我花了相当长的时间阅读了大量文档。 Logrotate 似乎无法对文件名中包含日期的不同文件进行分组。 Logrotate 不能做我们需要它做的事情。

您有两个选项可以更改 java / tomcat 提供的日志记录功能,使其不在文件名中包含日期。
http://tomcat.apache.org/tomcat-6.0-doc/logging。 第二种更快的方法是使用您自己的小脚本来为

您完成工作,即使用 findhttps://serverfault.com/questions/256218/logrotation-when-filenames-includes-date< /a>, https://serverfault.com/a/256231/71120

查找/pathtologs/* -mtime +5 -exec rm {} \;

我选择了第二个选项,因为我们的开发人员已经在文件名中编码了日期。所以它需要保持这种状态。
-mtime +5 将 find 设置为仅查找早于 5 天的文件。

find的文档中。

文件数据最后一次修改是在 n*24 小时前。请参阅 -atime 的注释,了解舍入如何影响文件修改时间的解释。

根据评论更新

查找/pathtologs/* -mtime +5 -删除

如果您特别想删除,这是一种快速的方法。
如果您需要其他命令,您可以随时将 exec rm {} \; 替换为其他命令。

I spent a quite a while reading a lot of documentation. Logrotate does not seem to be able to group the different files with dates included in the name of the file. Logrotate can not do what we need it to do.

You have two options change the logging facility provided by java / tomcat to not include the date in the file name.
http://tomcat.apache.org/tomcat-6.0-doc/logging.html

The second and quicker way is to use your own little script to do the work for you, using find. https://serverfault.com/questions/256218/logrotation-when-filenames-includes-date, https://serverfault.com/a/256231/71120

find /pathtologs/* -mtime +5 -exec rm {} \;

I went with the second option, because our developers have coded for dates in the files names. So it needs to stay that way.
The -mtime +5 sets find to only look for files who are older then 5 days.

From find's documentation.

File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.

Updated as per comment

find /pathtologs/* -mtime +5 -delete

If you specifically want to delete, this is a quick way to do it.
If you need to some other command you can always replace the exec rm {} \; with something else.

神回复 2025-01-04 03:15:13

/etc/cron.d/rotate_tomcat_logs 中类似以下内容:

# delete every log file over 100 days old, and compress every log file over 1 day old.
00 1 * * * root ( find /opt/tomcat/logs -name \*log\* -name \*.gz -mtime +100 -exec rm -f {} \; >/dev/null 2>&1 )
05 1 * * * root ( find /opt/tomcat/logs -name \*log\* ! -name \*.gz -mtime +1 -exec gzip {} \; >/dev/null 2>&1 )

Something like this in /etc/cron.d/rotate_tomcat_logs:

# delete every log file over 100 days old, and compress every log file over 1 day old.
00 1 * * * root ( find /opt/tomcat/logs -name \*log\* -name \*.gz -mtime +100 -exec rm -f {} \; >/dev/null 2>&1 )
05 1 * * * root ( find /opt/tomcat/logs -name \*log\* ! -name \*.gz -mtime +1 -exec gzip {} \; >/dev/null 2>&1 )
旧人九事 2025-01-04 03:15:13

/路径/到/日志/*.log {
米索克
压缩
旋转 7
这种

类型的事情不能正常工作,因为正如其他人指出的那样,tomcat 有自己的日志轮换。您可以使用简单的 cron 来删除旧文件或关闭访问日志阀门上的轮换。通过关闭日志轮换(并可能更改文件名模式),上述 logrotate 和其他类似的配置将正常工作。

最重要的是,您应该使用 logrotate 或 tomcat 中内置的日志轮转,但不能同时使用两者。

/path/to/logs/*.log {
missingok
compress
rotate 7
}

this type of thing doesn't work normally because as others point out tomcat has its own log rotation. You can either use a simple cron to delete old files or turn off rotation on the access log valve. By turning off log rotation (and possible changing the filename patter), the above logrotate and other similar configs will work fine.

The bottom line is you should use logrotate or the built in log rotation in tomcat but not both at the same time.

自在安然 2025-01-04 03:15:13

要在旋转文件中包含日期,您可以使用“dateext”选项。

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

旋转的文件应创建类似于下面的内容

 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

唯一的缺点是您每天不能运行它多次,因为该文件将具有该日期的明确名称。

上面的示例来自我在 GC 上的 k8s 中运行的 Nginx docker 容器。 logrotate版本是3.11.0。

希望有帮助!

更新:
来自手册页 https://linux.die.net/man/8/logrotate

日期格式 格式字符串

使用类似于 strftime(3) 函数的表示法指定 dateext 的扩展名。仅允许使用 %Y %m %d 和 %s 说明符。默认值为-%Y%m%d。请注意,分隔日志名称和扩展名的字符也是日期格式字符串的一部分。系统时钟必须设置在 2001 年 9 月 9 日之后,%s 才能正常工作。请注意,此格式生成的日期戳必须按词法可排序(即,首先是年份,然后是月份,然后是日期。例如,2001/12/01 可以,但 01/12/2001 则不行,因为 01/11/ 2002 年的排名会较低,而较晚)。这是因为使用旋转选项时,logrotate 会对所有旋转的文件名进行排序,以找出哪些日志文件较旧且应删除。

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.

从此见与不见 2025-01-04 03:15:13

另外,您可以添加 crons 而不是硬编码 logrotate。

1 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +30 -delete
2 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +1 -exec gzip -q {} \;

这样,您将删除超过 30 天的日志,并压缩超过 1 天的日志。

Also, you can add crons instead hardcode logrotate.

1 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +30 -delete
2 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +1 -exec gzip -q {} \;

In this way, you will delete the logs older than 30 days, and zip the logs older than 1.

痴情换悲伤 2025-01-04 03:15:13

也许您可以按照中所述从日志文件名中删除日期
如何从 tomcat 日志中删除日期模式能够使用 logrotate 规则。

这至少对我的本地主机访问日志有用。

Probably you can remove the date from the log file names as described in
How to remove the date pattern from tomcat logs to be able to use logrotate rules.

This worked for me at least for localhost access log.

不语却知心 2025-01-04 03:15:13

好吧,我对任何答案都不完全满意,即使那些指出 logrotate 不支持这一点的答案(即只是删除由其他应用程序旋转的文件) 场景肯定是正确的(也许对该工具提出功能请求?)。

因此,我想分享我所采用的另一种方法。与“find /path/to/logs -mtime +7 -delete”解决方案不同,此解决方案不会在指定时间段后删除所有旧日志时间。因此,这里是一个示例单行 bash 命令,它仅在磁盘上留下最后 N 个日志(无论何时运行):

for f in `ls -1r | grep -E "^catalina\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.log$" | tail -n +$((N+1))`; do rm $f; done

最后,为了完全涵盖该主题,最后一个替代解决方案不是旋转日志文件(例如,在 Tomcat 中使用 rotatable=false - 请参阅其文档)并照常使用 logrotate,但不要忘记将其与<一href="https://unix.stackexchange.com/questions/33447/why-we-should-use-create-and-copytruncate-together/39509">'copytruncate' 选项。

欢迎提出建议...

Well, I was not fully satisfied with any of the answers, even though the ones stating that the logrotate doesn't support this (i. e. just to remove files rotated by some other application) scenario are surely correct (raise a feature request on that tool, maybe?).

So, I would like to share an alternative approach I've come to. Unlike the "find /path/to/logs -mtime +7 -delete" solution, this one won't remove all the old logs after a specified period of time. So here it comes, an example one-liner bash command which leaves just N last logs on the disk (whenever it is run):

for f in `ls -1r | grep -E "^catalina\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.log$" | tail -n +$((N+1))`; do rm $f; done

Finally, to cover the topic completely, the last alternative solution is not to rotate the log files (e. g. use rotatable=false in case of Tomcat - see its docs) and use the logrotate as usually, but don't forget to use it with the 'copytruncate' option.

Suggestions are welcome...

生活了然无味 2025-01-04 03:15:13

在日志轮换文件中,使用rotate #,其中 # 是在删除日志之前要保留的日志数量。

旋转次数

日志文件在被删除或之前会轮换多次
邮寄到邮件指令中指定的地址。如果计数是
0,旧版本被删除而不是旋转。

In your log rotate file, use rotate #, where # is the number of logs you want to keep before removing them.

rotate count

Log files are rotated times before being removed or
mailed to the address specified in a mail directive. If count is
0, old versions are removed rather then rotated.

鸵鸟症 2025-01-04 03:15:13

要在 tomcat [linux] 中启用每日日志轮换,并将日期后缀添加到 catalina 文件,请执行以下更改。

下载 cronolog 软件包并安装在 Linux 操作系统中

wget http://pkgs.fedoraproject.org/repo/pkgs/cronolog/cronolog-1.6.2.tar.gz/md5/a44564fd5a5b061a5691b9a837d04979/cronolog-1.6.2.tar.gz

tar -zxvf cronolog-1.6.2.tar.gz
./configure
make
make install 

打开使用 vi 命令修改 apache-tomcat-7.0.65/bin/catalina.sh 文件并更改以下行
示例:/opt/apache-tomcat-7.0.65/bin/catalina.sh

shift
touch "$CATALINA_OUT"
# comment above line as below 
#touch "$CATALINA_OUT"
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
  echo "Using Security Manager"
fi
shift
eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
  -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
  -Djava.security.manager \
  -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start \
  #>> "$CATALINA_OUT" 2>&1 "&"
  # comment above line and add below given line
2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &
else
eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
  -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start \
  #>> "$CATALINA_OUT" 2>&1 "&"         
  # comment above line and add below given line
2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &
fi

上述更改后保存文件并重新启动 tomcat 以应用更改。

To enable daily log rotation in tomcat [linux] with postfix of date to catalina file, do the below changes.

Download cronolog package and install in linux os

wget http://pkgs.fedoraproject.org/repo/pkgs/cronolog/cronolog-1.6.2.tar.gz/md5/a44564fd5a5b061a5691b9a837d04979/cronolog-1.6.2.tar.gz

tar -zxvf cronolog-1.6.2.tar.gz
./configure
make
make install 

open apache-tomcat-7.0.65/bin/catalina.sh file using vi command and change the lines as given below
example : /opt/apache-tomcat-7.0.65/bin/catalina.sh

shift
touch "$CATALINA_OUT"
# comment above line as below 
#touch "$CATALINA_OUT"
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
  echo "Using Security Manager"
fi
shift
eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
  -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
  -Djava.security.manager \
  -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start \
  #>> "$CATALINA_OUT" 2>&1 "&"
  # comment above line and add below given line
2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &
else
eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
  -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
  -Dcatalina.base="\"$CATALINA_BASE\"" \
  -Dcatalina.home="\"$CATALINA_HOME\"" \
  -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
  org.apache.catalina.startup.Bootstrap "$@" start \
  #>> "$CATALINA_OUT" 2>&1 "&"         
  # comment above line and add below given line
2>&1 |/usr/local/sbin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &
fi

After the above changes save the file and restart the tomcat to apply changes.

故事↓在人 2025-01-04 03:15:13

当前的答案可能有效,但我发现它们使事情变得太复杂,而不是使用 logrotate 已经可以很好地处理事情。如果 logrotate 已经处理了这个问题,那么您不需要手动指定命令。如果您只是解决 catalina 执行 logrotation 的初始问题并让 logrotate 处理它,则不需要复杂的配置。


看起来文件已经在旋转,如问题中日志文件名称中的日期所示:

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

那么,哪个组件已经做到了这一点,它是在 /etc/logrotate.d/tomcat* 中配置的吗?当前您系统上该文件的内容是什么?如果我没记错的话,catalina 可能已经这样做了:每天创建一个新的日志文件并将旧文件移动到新位置。如果您希望 logrotate 来处理此问题,您可能需要更改 tomcat / catalina 的默认行为,如此答案 首先。

之后,catalina 应始终写入 catalina.log

那么你就可以把轮转/压缩/删除完全交给logrotate来负责。

如果你想压缩旧文件,你可以将 compress 添加到 logrotate 配置中:

例如,你有:

/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 

}

你应该得到什么结果:

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

如果事情不起作用:

  • 使用 - 手动执行 logrotate v
  • 它还应该显示记录最后一次轮换的状态文件,例如 /var/lib/logrotate/status
  • 通常它会每天轮换一次(或您指定的任何内容)。您可以使用 -f 强制执行此操作。或者您可以操作状态文件(除非您知道自己在做什么,否则不要在生产中执行此操作)

一个常见的错误是人们手动运行 logrotate,然后如果没有任何改变就会感到困惑。 Logrotate 将仅执行它已配置为执行的操作。如果您指定每日轮换,则即使您按照需要经常运行它,它也不会再次轮换,除非您使用 -f。它将使用状态文件来跟踪。您可以使用 -v ,logrotate 将报告它正在做什么以及为什么。

资源:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文