How to configure application logging using Log4J on Tomcat 9?

a 夏天 提交于 2021-02-08 09:54:11

问题


I want to use Log4J to log from my Tomcat-based application.

The Log4J configuration is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">

    <appender name="roller" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${catalina.base}/logs/application.log"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[slf5s.start]%d{DATE}[slf5s.DATE]%n%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n"/>
        </layout>
    </appender>


    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="|%d|%5.5p|%5.5t|%20.20c{20}.%M - %m%n"/>
        </layout>
    </appender>

    <category name="com.acme" additivity="false">
        <priority value="trace"/>
        <appender-ref ref="roller"/>
        <appender-ref ref="console"/>
    </category>

</log4j:configuration>

I can see in the stdout/stderr logs that Log4J gets initialised and the expected logging file is created but it remains empty.

The other catalina logs are all created and filled. Is it possible that the JULI logging is preventing Log4J from working properly?


回答1:


I am migrating the project from Websphere to Tomcat and also introducing Maven.

The problem was that Maven dependencies was loading 2 SLF4J providers: log4j adapter and classic logback.

This resulted in the NopLogger being used which does not log anything.

I explicitly excluded the classic logback dependency and logging now works.



来源:https://stackoverflow.com/questions/56865690/how-to-configure-application-logging-using-log4j-on-tomcat-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!