在logging.properties中自定义日志格式
我需要一些在 Tomcat 7 中配置日志格式的指导。我在日志配置方面相对较新,所以请原谅幽默这个问题,如果它看起来有点基本...
使用在logging.properties中配置的Tomcat中的标准日志记录显示一个日志格式:
Jun 6, 2011 9:27:00 AM com.class.Control_WS callWebService
INFO: Response received from Control_WS:[Y]
我想自定义这些日志以压缩为一行,并扩展日期格式以包括毫秒。
示例:
[2011-05-04T11:37:00.037|INFO|javax.enterprise.system.stream.out|Response recieved from Control_WS:[Y]]
我可以使用 JUL 执行此操作还是需要切换到 LOG4J?
任何可以提供的简单示例或指导将不胜感激。
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...
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.
Example:
[2011-05-04T11:37:00.037|INFO|javax.enterprise.system.stream.out|Response recieved from Control_WS:[Y]]
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Java 7(或更高版本:-)),您不再需要为
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'sSimpleFormatter
prints the information. So as long as you are using SimpleFormatter (which is the default anyway) then this will work.Some pitfalls:
看起来您可以通过创建一个扩展 格式化程序,然后在您的logging.properties中将其指定为处理程序的格式化程序。例如,如果您使用 ConsoleHandler 你会使用这个:
然后将类文件(单独或放在 jar 中)放置在 tomcat 的类路径中的某个位置。我会创建一个包含它的 jar,然后将该 jar 放入
${catalina.home}/lib/ext
It looks like you would do this by creating a custom class that extends Formatter and then specify it in your logging.properties as the formatter for your handler. For example, if you are using a ConsoleHandler you would use this:
Then place the class file (either by itself, or inside a jar) somewhere in the classpath of tomcat. I would create a jar with it in there and then put that jar in
${catalina.home}/lib/ext