How do I configure Spring and SLF4J so that I can get logging?

84,639

Solution 1

In addition to Jatin's answer:

Spring uses Jakarta Commons Logging as a logging API. In order to log to slf4j, you need to make sure commons-logging is not on the classpath. jcl-over-slf4j is a replacement jar for commons-logging.

If you're using maven, you can detect where commons-logging comes from using mvn dependency:tree and exclude it from all dependencies that require it using dependency exclusions. You might need to run mvn dependency:tree several times though, because it only shows the first occurence of a transitive dependency.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

Solution 2

You'll find an example at https://github.com/mbogoevici/spring-samples/tree/master/mvc-basic/trunk. You need to include some dependencies in your POM file to enable logging.

<!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>runtime</scope>
    </dependency>

Solution 3

Just for the sake of completeness, a logback-classic variant:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.6</version>
    <scope>runtime</scope>
</dependency>

Do not forget however to disable commons-logging dependency which sprouts from Spring dependency like in the accepted (Stijn's) answer.

Solution 4

Use blow configuration for implementation of the JCL API on the classpath:

<dependencies>
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>3.0.0.RELEASE</version>
          <scope>runtime</scope>
          <exclusions>
             <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
          </exclusions>
       </dependency>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>jcl-over-slf4j</artifactId>
          <version>1.5.8</version>
          <scope>runtime</scope>
       </dependency>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.5.8</version>
          <scope>runtime</scope>
       </dependency>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.5.8</version>
          <scope>runtime</scope>
       </dependency>
       <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.14</version>
          <scope>runtime</scope>
       </dependency>
    </dependencies> 

for More information check here

Solution 5

keep log4j file in default package

Share:
84,639

Related videos on Youtube

Programming Guy
Author by

Programming Guy

Updated on June 05, 2020

Comments

  • Programming Guy
    Programming Guy almost 4 years

    I've got a maven & spring app that I want logging in. I'm keen to use SLF4J.

    I want to put all my config files into a directory {classpath}/config including log4j.xml and then init using a spring bean.

    e.g.

    <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
        <property name="targetMethod" value="initLogging"/>
        <property name="arguments">
            <list>
                <value>classpath:config/log4j.xml</value>
            </list>
        </property>
    </bean>
    

    However I get this warning and no logging.

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

    I've googled around and can't find a simple example on setting this up. Any ideas?

    • JoseK
      JoseK almost 14 years
      I'm guessing. Does it work any different if you try a complete path <value>C:/config/log4j.xml</value>
    • Programming Guy
      Programming Guy almost 14 years
      Nope. Hard coding doesn't help. =(
  • Programming Guy
    Programming Guy almost 14 years
    The example has the log4j config file directly in the classpath, not under a config directory which is what I'd like.
  • Programming Guy
    Programming Guy almost 14 years
    Actually that did help a bit though. I've now got logging in my app, It's just spring that's complaining.
  • Jatin
    Jatin almost 14 years
    If a particular class is complaining you need to add the required package to the list of packaged registered with the logger. You can also put it in a config folder and add that folder to the class path.
  • Programming Guy
    Programming Guy almost 14 years
    I think the warning is start up only. Once the log4j configurer bean has run I'm getting all the logging I need. Would be nice to be able to make the warning go away, but I think I'm ok to leave it.
  • Andrew B
    Andrew B about 9 years
    I like the third approach suggested here - slf4j.org/faq.html#excludingJCL - using empty artifacts. This option is recommended in the current version of the Spring Reference manual.