过滤掉log4j2文件中的hibernate日志

时间:2015-01-29 16:18:59

标签: java hibernate logging log4j log4j2

我有一个log4j2.xml文件,如下所示。它正在运行,但它已经收集了大量的hibernate和GWT日志文件。如何排除这些包,或者,如何让我的日志文件只包含我的包hiearchy中的日志记录?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="FileAllLogs" fileName="logs/MyProj_all.txt" immediateFlush="true" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <File name="FileAllErrors" fileName="logs/MyProj_errors.txt" immediateFlush="true" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="FileAllLogs"/>
        </Root>
        <Root level="error">
            <AppenderRef ref="FileAllErrors"/>
        </Root>
    </Loggers>
</Configuration>

1 个答案:

答案 0 :(得分:8)

删除你的&#34;调试&#34;根记录器。然后添加以下记录器,它将仅记录来自给定&#34; some.dummy.package&#34;

的日志
<logger name="some.dummy.package" level="debug" additivity="false">
    <AppenderRef ref="Console" />
    <AppenderRef ref="FileAllLogs"/>
 </logger>

所以最后你会有

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
    <File name="FileAllLogs" fileName="logs/MyProj_all.txt" immediateFlush="true" append="true">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
    <File name="FileAllErrors" fileName="logs/MyProj_errors.txt" immediateFlush="true" append="true">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</Appenders>
<Loggers>
    <Root level="error">
        <AppenderRef ref="FileAllErrors"/>
    </Root>
    <Logger name="some.dummy.package" level="debug" additivity="false">
         <AppenderRef ref="Console" />
         <AppenderRef ref="FileAllLogs"/>
    </Logger>
</Loggers>
</Configuration>