在logging.properties中自定义日志格式 [英] Customizing log format in logging.properties
问题描述
我在Tomcat 7中配置日志格式需要一些指导。我在日志记录配置方面比较新,所以请原谅幽默这个问题,如果看起来有点基础......
I need some direction in configuring the log format in Tomcat 7. I am relatively new at logging configurations so please excuse humor this questions if it seems a bit basic...
在logging.properties中配置的Tomcat中使用标准日志记录显示以下格式的日志:
Using the standard logging in Tomcat configured in logging.properties displays a log in the format of:
Jun 6, 2011 9:27:00 AM com.class.Control_WS callWebService
INFO: Response received from Control_WS:[Y]
我想自定义这些日志以压缩到一行,并将日期格式扩展到包括毫秒。
I would like to customize these logs to compress on to one line as well as expanding the date format to include milliseconds.
示例:
[2011-05-04T11:37:00.037|INFO|javax.enterprise.system.stream.out|Response recieved from Control_WS:[Y]]
这是我可以用JUL做的事情还是我需要切换到LOG4J?
Is that something I can do using the JUL or do I need to switch to LOG4J?
任何可以提供的简单示例或方向都会非常感激。
Any simple examples that can be provided or direction would be much appreciated.
推荐答案
如果您使用的是Java 7(或更高版本:-)),则无需再为<$创建自定义格式化程序类c $ c> java.util.logging (JUL)。在Java 7中有一个新属性, java.util.logging.SimpleFormatter.format
,它控制JUL的 SimpleFormatter
打印信息。因此,只要您使用的是SimpleFormatter(无论如何都是默认值),那么这将有效。
If you are using Java 7 (or later :-)) you no longer have to create a custom formatter class for java.util.logging
(JUL). In Java 7 there's a new property, java.util.logging.SimpleFormatter.format
, which controls how JUL's SimpleFormatter
prints the information. So as long as you are using SimpleFormatter (which is the default anyway) then this will work.
一些陷阱:
- 确保实际使用Java 7. :-)
- 请务必使用有效的格式字符串。如果静默恢复为默认格式(标准丑陋的双线)您提供的util / logging / SimpleFormatter.html#formatrel =noreferrer>格式无效。
- Be sure to actually use Java 7. :-)
- Be sure to use a valid format string. JUL will revert silently to the default format (the standard ugly two-liner) if the format you provide is invalid.
这篇关于在logging.properties中自定义日志格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!