修改Tomcat下的java.util.logging.SimpleFormatter格式属性

rya*_*all 15 tomcat java.util.logging

我正在使用Tomcat 7.0.28,在Ubuntu上的OpenJDK 1.7下运行,我正在尝试修改java.util.logging.SimpleFormatter使用的格式化字符串.根据该类的Javadocs,我可以指定属性java.util.logging.SimpleFormatter.format来更改格式.事实上,当我在Eclipse中运行我的webapp并在我的logging.properties文件中更改此属性时,它可以工作.

但是,当我将应用程序部署到Tomcat时,此属性似乎没有任何效果.我确信我的属性文件正在被正确读取,因为我对它做的其他更改确实生效了(我正在使用文件读取属性)

LogManager.getLogManager().readConfiguration(new FileInputStream(file))
Run Code Online (Sandbox Code Playgroud)

where文件是通过我的web.xml文件中的参数配置的.我已经尝试将该文件放在WEB-INF/classes/logging.properties中,但行为没有变化.

SimpleFormatter的Javadocs指定如果属性文件和系统属性都指定格式化字符串,则系统属性优先.我已验证未设置系统属性

context.log ("Formatting system property is " + System.getProperty("java.util.logging.SimpleFormatter.format"));
Run Code Online (Sandbox Code Playgroud)

在ServletContextListener.contextInitialized方法中.

这是我的记录属性文件

handlers=java.util.logging.ConsoleHandler

#  Default logging level for root logger
.level=FINE

#  Set the level for the ConsoleHandler
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tr] %3$s %4$s:  %5$s %n
Run Code Online (Sandbox Code Playgroud)

我已经尝试了我能想到的一切,包括修改TOMCAT/conf和JRE_HOME/lib目录中的logging.properties.似乎没有任何区别.

小智 9

感谢nolan6000指出的bug报告信息, 我终于得到了tomcat-juli的工作.

代替:

java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
Run Code Online (Sandbox Code Playgroud)

它一定要是:

1catalina.java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
Run Code Online (Sandbox Code Playgroud)


小智 7

java.util.logging.SimpleFormatter.format 确实可以在logging.properties中设置 - 这对我来说也不起作用 - 或者作为命令行参数(java选项).

命令行参数似乎对我有用.因为$ JAVA_OPTS变量经历了如此多的篡改并最终得到了eval,这就是我解决它的方法(在Debian上,java 1.7.0_07,apache-tomcat-7.0.30)

$ CATALINA_HOME/bin/catalina.sh(第230行):

JAVA_OPTS="$JAVA_OPTS \"-Djava.util.logging.SimpleFormatter.format=%1\\\$tY-%1\\\$tm-%1\\\$td %1\\\$tH:%1\\\$tM:%1\\\$tS.%1\\\$tL %4\\\$s %3\\\$s %5\\\$s%6\\\$s%n\""
Run Code Online (Sandbox Code Playgroud)


pet*_*erh 7

您可能正在目睹这个错误.

从Tomcat版本7.0.41开始以及6.0.38开始修复该错误.


小智 3

不知道这是否能解决您的问题,但可能值得一试。我看到了相同的行为,尽管在我的例子中,我是以编程方式进行日志记录设置而不是使用属性。事实证明,需要在构造(在我的例子中)FileHandler 之前设置 java.util.logging.SimpleFormatter.format 的属性值。我在构造 FileHandler 之后、构造 SimpleFormatter 之前设置它。我想知道在您的属性文件中,在定义任何处理程序属性之前定义 java.util.logging.SimpleFormatter.format 是否可以解决问题。