JUL日志消息和Apache log4j2 log4j-jul日志记录为org.apache.commons.logging.impl.Jdk14Logger

时间:2015-10-13 15:55:58

标签: java log4j2 java.util.logging apache-commons-logging

我有log4j2使用tomcat-jdbc的应用程序。 tomcat-jdbc类都使用java.util.logging (JUL)进行日志记录,因此我查看了它,看到我可以使用以下依赖项和JVM参数将所有JUL日志调用路由到log4j2

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>${log4j.version}</version>
    </dependency>

JVM参数-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

总的来说,我的maven日志配置是:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <log4j.version>2.3</log4j.version>
    <oracle.ojdbc.version>11.2.0</oracle.ojdbc.version>
    <spring.framework.version>4.1.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
    <!-- Logging using log4j 2.x -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <!-- snipped non log4j related dependencies -->
<dependencies>

当我运行应用程序时,我现在从log4j2日志中获取来自tomcat-jdbc的消息,但我无法分辨它们的来源,因为来自log4j-jul的LogManager正在覆盖该信息:

2015/10/13 08:00:23.358 DEBUG 20512[main] org.apache.commons.logging.impl.Jdk14Logger log:99 - Executing prepared SQL query
2015/10/13 08:00:23.360 DEBUG 20512[main] org.apache.commons.logging.impl.Jdk14Logger log:99 - Executing prepared SQL statement [...]
2015/10/13 08:00:23.362 DEBUG 20512[main] org.apache.commons.logging.impl.Jdk14Logger log:99 - Fetching JDBC Connection from DataSource
2015/10/13 08:00:23.415 DEBUG 20512[main] org.apache.commons.logging.impl.Jdk14Logger log:99 - Returning JDBC Connection to DataSource

如何从原始邮件中获取信息?

我知道Log4perl有一个你可以调整的变量,如果你正在拦截一个事件(通过信号处理程序等):$Log::Log4perl::caller_depth++;,这将有助于此。不一定确定这是否有用,因为重新路由日志记录的代码是从第三方库(org.apache.logging.log4j / log4j-jul)完成的......

谢谢!

0 个答案:

没有答案