0
votes

I am working on a Spring-MVC application in which I am using Jetty as our application server. I want to debug log a specific java class, but even after enabling log4j or slf4j, and adding files it's not working.

Finally, I created a war file, added to start log4j with this command java -jar start.jar --add-to-start=logging-log4j, which created a log4j.xml file in resources directory. In that log4j.xml, I added the class I want to debug, but no [DEBUG] entries are getting added.

Enabled modules :

log4j2-slf4j.mod
slf4j-log4j2.mod
slf4j-api.mod
logging-slf4j.mod

I have also tried their combinations and different loggings as documentation suggested.

log4j.xml present in resources directory of both IDE and Jetty.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
                     debug="false">

    <!-- console appender -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelMatchFilter">
            <param name="LevelToMatch" value="INFO" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
        <filter class="org.apache.log4j.varia.DenyAllFilter"/>
    </appender>


    <logger name="com.zaxxer.hikari" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="console" />
    </logger>


    <root>
        <priority value="ERROR" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

What am I doing wrong?

1
You seem to have the bridges, but not log4j itself. Is log4j itself in your classpath? I recommend logback instead of log4j by the way, but that is another matter. - Jan Larsen
@JanLarsen : You are right, and I have logback in maven, ch.qos.logback. I don't want to add log4j in maven as I can see I have added them in exclusion list. How can i configure logback for my requirement? Thank you. - We are Borg

1 Answers

0
votes

It seems that the problem is that you have only bridges and not log4j itself in the classpath.

As requested a simple logback.xml config file:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <logger name="test" level="DEBUG" />

    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>