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

问题描述 投票:12回答:3

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

log.info("Logging pointless information...")

将输出所有这些:

Oct 26, 2011 9:37:57 PM java.util.logging.LogManager$RootLogger log
INFO: Logging pointless information...

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

java logging java.util.logging
3个回答
5
投票

你需要create a a different Formatter并使用它。

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

    @Override 
    public String format(final LogRecord record) 
    {
        return record.getMessage();
    }   
}

13
投票

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

-Djava.util.logging.SimpleFormatter.format='%4$s: %5$s%6$s%n'

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

-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'

3
投票

这些问题几乎是同一个问题:

这是一个如何实现Jarrod Roberson的建议的例子:http://www.javalobby.org/java/forums/t18515.html

通常,要应用格式化程序,您需要创建一个文件,通常称为logging.properties。在它中放一个这样的一行:

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

或者格式化程序类。然后像这样添加一个JVM arg:

-Djava.util.logging.config.file=logging.properties

或者使用更强大的日志记录系统,如Logback或Log4j,它们具有预构建的格式化程序,可以为您节省一些编码。

© www.soinside.com 2019 - 2024. All rights reserved.