隐藏log4j日志

时间:2016-02-13 02:58:48

标签: log4j

我使用log4j显示日志...

我正在使用xml配置,这是我使用的log4j.xml:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="stderr" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="warn" />
        <param name="target" value="System.err"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
        </layout>
    </appender>
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="debug" />
        <param name="target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="info" />
        </filter>
    </appender>
    <root>
        <priority value="ERROR"></priority>
        <appender-ref ref="stderr" />
        <appender-ref ref="stdout" />
    </root>
</log4j:configuration>

当我执行主类时,我想在我的控制台中只有错误日志..但是Log4j仍然显示它的日志:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@72ea2f77.
log4j: Using URL [file:/Users/***/jpa-model/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "false".

我是否有可能隐藏log4j日志?

我试图自己启动记录器并将级别设置为OFF但log4j仍然显示其正确的日志:

private static final Logger log = Logger.getLogger(HibernateTest.class.getName());

public static void main(String[] args) {
    log.setLevel(Level.OFF); 

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下,为我工作:

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %-6p [%c] %m%n
log4j.appender.console.filter.1=org.apache.log4j.varia.StringMatchFilter
log4j.appender.console.filter.1.StringToMatch=Artifact
log4j.appender.console.filter.1.AcceptOnMatch=true
log4j.appender.console.filter.2=org.apache.log4j.varia.DenyAllFilter