java.util.logging:如何抑制日期行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2950704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.logging: how to suppress date line
提问by andrewz
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:
在 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
I would like to get rid of the Jun 1, 2010...
lines, they just clutter my log output. How can I do this?
我想摆脱这些Jun 1, 2010...
行,它们只会使我的日志输出变得混乱。我怎样才能做到这一点?
采纳答案by andrewz
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:
该问题是由父日志中的处理程序引起的。解决方案是从父日志中删除所有处理程序,然后添加自己的自定义处理程序。此代码从父日志中删除处理程序:
for(Handler iHandler:log.getParent().getHandlers()) { log.getParent().removeHandler(iHandler); }
回答by Abhinav Sarkar
Write a custom formatter extending java.util.logging.Formatter
class and implement the String 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):
编写自定义格式化程序扩展java.util.logging.Formatter
类并String format(LogRecord)
根据您的需要实现该方法。例如,以下格式化程序仅显示日志消息(如果正在记录异常,则显示可抛出的堆栈跟踪):
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
class CustomRecordFormatter extends Formatter {
@Override
public String format(final LogRecord r) {
StringBuilder sb = new StringBuilder();
sb.append(formatMessage(r)).append(System.getProperty("line.separator"));
if (null != r.getThrown()) {
sb.append("Throwable occurred: "); //$NON-NLS-1$
Throwable t = r.getThrown();
PrintWriter pw = null;
try {
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw);
t.printStackTrace(pw);
sb.append(sw.toString());
} finally {
if (pw != null) {
try {
pw.close();
} catch (Exception e) {
// ignore
}
}
}
}
return sb.toString();
}
}
This is how you use it:
这是你如何使用它:
import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;
class A {
private static final Logger LOGGER = Logger.getLogger(A.class.getName());
static {
CustomRecordFormatter formatter = new CustomRecordFormatter();
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(formatter);
LOGGER.addHandler(consoleHandler);
}
public void doSomething() {
LOGGER.info("something happened");
}
}
回答by Demis Gallisto
From Java SE 7 there is a new system property: java.util.logging.SimpleFormatter.format.
从 Java SE 7 开始,有一个新的系统属性: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 属性文件 (logging.properties) 上配置。如果您是 Eclipse 用户,并且对控制台输出中的双行消息感到恼火,则可以通过以下方式更改 jre logging.properties 文件 (JDK_HOME/jre/lib/logging.properties):
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
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.
此处提供了一些示例格式:http: //docs.oracle.com/javase/7/docs/api/index.html?java/util/logging/SimpleFormatter.html。