如何将致命错误的时间戳添加到Java致命错误日志文件名中

发布于 2025-02-09 07:14:57 字数 580 浏览 0 评论 0原文

背景

  • 您可以使用Java -XX:errorfile =/path/to/file来指定致命错误日志文件的路径。
  • 您可以使用-XX:ONERROR选项在Java VM上导致错误时执行命令。
  • 为了保持致命的错误日志,我想将致命错误发生的时间戳添加到文件名中。

问题

我试图设置这样的Java VM选项:

java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" -jar /path/to/application.jar

但是,当Java VM上发生错误时,文件名上的时间戳是Java VM启动的时间,而不是致命错误发生的时间。

问题

是否有任何方法可以将致命错误发生的时间戳添加到Java致命误差日志文件名中?

Background

  • You can use java -XX:ErrorFile=/path/to/file to specify the path of fatal error log file.
  • You can use the -XX:OnError option to execute a command when an error causes on a Java VM.
  • To maintain the fatal error logs, I want to add the timestamp of the fatal error occurrence to the filename.

Problem

I have tried to set the Java VM option like this:

java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" -jar /path/to/application.jar

But when an error occurs on Java VM, the timestamp on the filename is the time of the Java VM's startup, rather than the time of occurrence of the fatal error.

Question

Is there any way to add the timestamp of the fatal error occurrence to the Java fatal error log filename?

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

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

发布评论

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

评论(1

携余温的黄昏 2025-02-16 07:14:57

我找到了一个解决方案。为了使命令扩展以后有效,您必须将$作为字面形式传递(使用\ $(...))类似:

java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_\$(date +%F-%H%M%S).log" -jar /path/to/application.jar

当发生致命错误时,执行以下命令:

# An error report file with more information is saved as:
# /path/to/error.log
#
# -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log"
#   Executing /bin/sh -c "mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" ...
Aborted

I found a solution. To make the command expansion works later, you have to pass $ as a literal (use \$(...)) like:

java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_\$(date +%F-%H%M%S).log" -jar /path/to/application.jar

and when a fatal error occurred, the following command was executed:

# An error report file with more information is saved as:
# /path/to/error.log
#
# -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log"
#   Executing /bin/sh -c "mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" ...
Aborted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文