0

When the servers starts up, I see all the correct log4j configuration in the console. However, I don't see any log messages from spring framework.

Also, I added some code that outputs defined loggers, and I see that my spring logger is defined. Why don't I see any log messages from spring?

Enumeration loggers = Logger.getDefaultHierarchy().getCurrentLoggers();
while( loggers.hasMoreElements()){
    Logger logger = (Logger) loggers.nextElement();
    System.out.println(logger.getName()+","+logger.getLevel());
}

SystemOut     O org.springframework,DEBUG

My web.xml is (log4j/spring section):

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/resource/log4j.xml</param-value>
</context-param>

 <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

My log4j.xml is:

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

<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="conversionPattern" value="%5p %d %C{1} - %m%n" />
        </layout>
    </appender>

    <!-- Loggers to filter out various class paths -->
    <logger name="org.springframework" additivity="false">
        <level value="debug"/>
        <appender-ref ref="ConsoleAppender" />
    </logger>

</log4j:configuration>

Here's what I see in the console:

SystemOut     O log4j: System property is :null
SystemOut     O log4j: Standard DocumentBuilderFactory search succeded.
SystemOut     O log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
SystemOut     O log4j: debug attribute= "true".
SystemOut     O log4j: Threshold ="null".
SystemOut     O log4j: Retreiving an instance of org.apache.log4j.Logger.
SystemOut     O log4j: Setting [org.springframework] additivity to [false].
SystemOut     O log4j: Level value for org.springframework is  [debug].
SystemOut     O log4j: org.springframework level set to DEBUG
SystemOut     O log4j: Class name: [org.apache.log4j.ConsoleAppender]
SystemOut     O log4j: Setting property [target] to [System.Out].
SystemOut     O log4j: Setting property [threshold] to [DEBUG].
SystemOut     O log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
SystemOut     O log4j: Setting property [conversionPattern] to [%5p %d %C{1} - %m%n].
SystemOut     O log4j: Adding appender named [ConsoleAppender] to category [org.springframework].
SystemOut     O log4j: Level value for root is  [debug].
SystemOut     O log4j: root level set to DEBUG
SystemOut     O log4j: Adding appender named [ConsoleAppender] to category [root].

Things I've tried:

1) An accepted answer from this post

2) I removed a lot of jars that I thought might interfere with log4j.

  • I replaced slf4j-jdk.jar with slf4j-log4j.jar

3) Adding springs log4j listener to web.xml (also tried log4jInitialization bean in appcontext.xml)

Community
  • 1
  • 1
user2793390
  • 741
  • 7
  • 29

4 Answers4

0

Try adding "Target" attribute to your Console appender like this:

 <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="conversionPattern" value="%5p %d %C{1} - %m%n" />
        </layout>
    </appender>

P.S. Example from my log4j.xml (I use it on JBoss 7) file:

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

    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="debug"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
        </layout>
    </appender>

    <logger name="org.springframework" additivity="false">
        <level value="debug"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <root>
        <level value="WARN"/>
        <appender-ref ref="consoleAppender"/>
    </root>

</log4j:configuration>
Ernestas Kardzys
  • 1,719
  • 2
  • 16
  • 21
0

You need to specify root logger.

<root>
    <priority value="info"/>
    <appender-ref ref="ConsoleAppender"/>
</root>

Also, as @Ernestas said, ConsoleAppender needs Target attribute.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
0

Turns out the missing piece was jcl-over-slf4j.jar. Once I had it working, I started backing out all other changes I made to see which ones were unnecessary. Ultimately, this is what I had to do:

  1. Add: jcl-over-slf4j.jar, slf4j-log4j12.jar
  2. Remove: slf4j-jdk.jar (commons-logging.jar didn't interfere, so it stayed.)
  3. Add log4j.xml (see above)

log4j specific configuration in web.xml or appcontext.xml turned out to be unnecessary - at least in my case.

jar files in my lib folder that I found to have their own log4j.properties files did not interfere either - log4j interference - level keeps being set to "OFF"

Btw, the following post was very helpful in identifying those files - List contents of multiple jar files

Community
  • 1
  • 1
user2793390
  • 741
  • 7
  • 29
0

I also had the same issue, I fixed this by adding the dependency of commons-logging please try if this works for you as well. below is the dependency I added in pom.xml.

       <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.0.3</version>
       </dependency>
Anish Lodhi
  • 321
  • 1
  • 4
  • 7