java.util.logging:如何抑制日期线
在 java.util.logging 中使用默认记录器时,我试图在日志记录期间抑制日期行的输出。例如,这是一个典型的输出:
Jun 1, 2010 10:18:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:18:12.0, local-time=20100601t101812, duration=180000
Jun 1, 2010 10:21:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:21:12.0, local-time=20100601t102112, duration=180000
我想删除 Jun 1, 2010...
行,它们只会扰乱我的日志输出。我该怎么做?
I'm trying to suppress output of the date line durinng logging when using the default logger in java.util.logging. For example, here is a typical output:
Jun 1, 2010 10:18:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:18:12.0, local-time=20100601t101812, duration=180000
Jun 1, 2010 10:21:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:21:12.0, local-time=20100601t102112, duration=180000
I would like to get rid of the Jun 1, 2010...
lines, they just clutter my log output. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Java SE 7 开始,有一个新的系统属性:java.util.logging.SimpleFormatter.format。
还可以在 java.util.logging 属性文件 (logging.properties) 上配置相同的属性。
如果您是 Eclipse 用户,并且对控制台输出中的双行消息感到恼火,则可以按以下方式更改 jrelogging.properties 文件 (JDK_HOME/jre/lib/logging.properties)
: util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
此处提供了一些示例格式:http://docs.oracle.com/javase/7/docs/api/index.html?java/util/日志记录/SimpleFormatter.html。
From Java SE 7 there is a new system property: java.util.logging.SimpleFormatter.format.
The same property is also configurable on the java.util.logging properties file (logging.properties).
If you are an Eclipse user, and you are annoyed by the double line message in the console output, you could change the jre logging.properties file (JDK_HOME/jre/lib/logging.properties) in this way:
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
Some example format is available here: http://docs.oracle.com/javase/7/docs/api/index.html?java/util/logging/SimpleFormatter.html.
该问题是由父日志中的处理程序引起的。解决方案是从父日志中删除所有处理程序,然后添加自己的自定义处理程序。此代码从父日志中删除处理程序:
The problem is caused by a handler in the parent log. The solution is to remove all handlers from the parent log, and then add own custom handler. This code removes handlers from the parent log:
编写一个扩展
java.util.logging.Formatter
类的自定义格式化程序,并根据您的需要实现String format(LogRecord)
方法。例如,以下格式化程序仅显示日志消息(如果正在记录异常,则显示可抛出的堆栈跟踪):这是使用它的方式:
Write a custom formatter extending
java.util.logging.Formatter
class and implement theString format(LogRecord)
method according to your needs. For example, the following formatter shows only the log message (and the throwable stacktrace if an exception is being logged):This is how you use it: