Java 如何在不禁用进一步日志记录的情况下清除 catalina.out?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37094871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:59:20  来源:igfitidea点击:

How to clear catalina.out without disabling further logging?

javatomcat

提问by Jibby

Every once in a while our catalina.out file gets very very large (yes, I am implementing slf4j and logback in my applications to prevent this in the future). But for now, when I go to cycle the logs, I copy catalina.out to catalina.{date} and execute cat /dev/null > catalina.out. The problem is, tomcat will capture no further logs after I do that, until tomcat is restarted the next morning, and this is not ideal. Why does this happen? And is there a way to avoid it?

每隔一段时间我们的 catalina.out 文件就会变得非常大(是的,我正在我的应用程序中实现 slf4j 和 logback 以防止将来发生这种情况)。但是现在,当我去循环日志时,我将 catalina.out 复制到 catalina.{date} 并执行cat /dev/null > catalina.out. 问题是,在我这样做之后,tomcat 将不再捕获更多日志,直到第二天早上重新启动 tomcat,这并不理想。为什么会发生这种情况?有没有办法避免它?

采纳答案by Michael-O

Easy as cake: echo > catalina.out. The file descriptor won't change and javacan continue to write to that file.

像蛋糕一样简单:echo > catalina.out。文件描述符不会改变并且java可以继续写入该文件。

回答by Kedar Mhaswade

What you are looking for is log rotation. Since this requires a low-level signal supportwhile the process is running, you should do this indirectly. You can use this procedureto achieve this. This is also documented on Tomcat Wiki.

您正在寻找的是日志轮换。由于这在进程运行时需要低级信号支持,因此您应该间接执行此操作。您可以使用此过程来实现此目的。这也记录在Tomcat Wiki 上

回答by Edwin Buck

The traditional way is to

传统的方法是

cat /dev/null > catalina.out

It will clear the log file, and will not disrupt the processes that currently hold open file handles.

它将清除日志文件,并且不会中断当前持有打开文件句柄的进程。

The better way is to not lose your logging information, by rotating out the log file. To do this, create or edit the file /etc/logrotate.d/tomcatand have its contents read

更好的方法是通过轮换日志文件来不丢失日志信息。为此,请创建或编辑文件/etc/logrotate.d/tomcat并读取其内容

/var/log/tomcat/catalina.out {   copytruncate   daily   rotate 7   compress   missingok   size 5M  }

Then restart logrotate with the command (as root)

然后使用命令重新启动 logrotate(以 root 身份)

/usr/sbin/logrotate /etc/logrotate.conf

And you should have the log file rotated out daily, or if the size exceeds 5M, with the last seven logs kept for debugging purposes.

并且您应该每天轮换日志文件,或者如果大小超过 5M,则保留最后七个日志以用于调试目的。

回答by Jose Martinez

You can truncate the file. This makes logical sense also since it is essentially what you are trying to do.

您可以截断文件。这也是合乎逻辑的,因为它本质上就是你想要做的。

truncate -s 0 M catalina.out

truncate -s 0 M catalina.out

FYI: Doing a cat /dev/null > filedoes not alter the inode of the file.

仅供参考:执行 acat /dev/null > file不会改变文件的 inode。

logs]$ls -i test.log
19794063 test.log
logs]$
logs]$cat /dev/null > test.log
logs]$ls -i test.log
19794063 test.log

Also, I had a separate command tailing live data into test.logduring these commands. After running these commands the tail to test.logstill worked without a problem. This does not answer your question as to why it stopped working, but it helps rule out change in inode.

此外,我有一个单独的命令test.log在这些命令期间将实时数据拖尾。运行这些命令后,tailtest.log仍然可以正常工作。这并没有回答您关于它为什么停止工作的问题,但它有助于排除 inode 的变化。