如何在 tomcat catalina.out 日志文件中显示日期和时间

car*_*l S 6 tomcat7

我需要在我的 tomcat7 安装的 catalina.out 日志文件中显示实际日志信息的日期和时间。从网上我找到了将这一行添加到 logging.properties 文件的解决方案,但它不起作用。我添加了以下内容:

1catalina.java.util.logging.SimpleFormatter.format=[%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS,%1$tL
Run Code Online (Sandbox Code Playgroud)

现在它只是一堆零时间戳的数据。我只想要日志输出中INFOERRORyyyymmdd hhmmss之前的标准。

现在在我的 logging.properties 文件中是这样的(我显然添加了最后一行):

java.util.logging.ConsoleHandler.level = FINE

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

1catalina.java.util.logging.SimpleFormatter.format=[%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS,%1$tL

the version Im using is apache-tomcat-7.0.82.
Run Code Online (Sandbox Code Playgroud)

您能提供的任何帮助都会很棒,并在此先感谢您。

小智 2

这是在 Tomcat 7.0.99 上对我有用的:

...
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8

# My special format:
java.util.logging.SimpleFormatter.format=%1$tF %1$tT [%4$-7s] %5$s %n
...
Run Code Online (Sandbox Code Playgroud)

前三行是开箱即用的默认设置。最后一行定义了 SimpleFormatter。日志文件如下所示:

2020-02-03 11:55:26 [INFORMATION] Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0]. 
2020-02-03 11:55:26 [INFORMATION] APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 
2020-02-03 11:55:26 [INFORMATION] OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019] 
2020-02-03 11:55:26 [INFORMATION] Initialisiere ProtocolHandler["http-apr-8080"] 
2020-02-03 11:55:26 [INFORMATION] Initialisiere ProtocolHandler["ajp-apr-8009"] 
2020-02-03 11:55:26 [INFORMATION] Initialization processed in 368 ms 
2020-02-03 11:55:26 [INFORMATION] Starting service [Catalina] 
2020-02-03 11:55:26 [INFORMATION] Starting Servlet Engine: Apache Tomcat/7.0.99 
Run Code Online (Sandbox Code Playgroud)

有关 SimpleFormatter 的更多信息,请参阅:Oracle Java 文档 - SimpleFormatter,有关 Formatter 的更多信息,请参阅:Oracle Java 文档 - Formatter

您甚至可以将 Tomcat 日志记录更改为 log4j (请参阅:Tomcat - using_Log4j),但这需要更多的努力,并且对于我的简单用例来说是一种矫枉过正。