如何从2行java.util.logging输出中抑制日期行?

Jam*_*s T 12 java logging java.util.logging

我正在使用Java默认记录器,现在它正在输出很多无用的垃圾,这里有一个例子,这行代码:

log.info("Logging pointless information...")
Run Code Online (Sandbox Code Playgroud)

将输出所有这些:

Oct 26, 2011 9:37:57 PM java.util.logging.LogManager$RootLogger log
INFO: Logging pointless information...
Run Code Online (Sandbox Code Playgroud)

除了第二行,我不需要知道任何事情.我怎样才能删除这些垃圾?我想要的只是简单的文本记录.

Tre*_*son 13

也许这是后来添加的,但至少在Java 7中,java.util.logging.SimpleFormatter支持从系统属性获取其格式.此JVM参数将仅打印第二行:

-Djava.util.logging.SimpleFormatter.format='%4$s: %5$s%6$s%n'
Run Code Online (Sandbox Code Playgroud)

就个人而言,我喜欢保留所有日期/来源信息,但摆脱换行符(并使用更紧凑的国际日期格式):

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

  • 你真的不知道你让我的生活变得多么美好。由于不熟悉 java,这至少为我节省了 5 个小时的工作。 (2认同)

小智 5

您需要创建一个不同的Formatter并使用它.

public class BriefFormatter extends Formatter 
{   
    public BriefFormatter() { super(); }

    @Override 
    public String format(final LogRecord record) 
    {
        return record.getMessage();
    }   
}
Run Code Online (Sandbox Code Playgroud)