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

后端 未结 7 928
借酒劲吻你
借酒劲吻你 2020-12-07 17:52

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 lo

相关标签:
7条回答
  • 2020-12-07 18:03

    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>
    
    0 讨论(0)
  • 2020-12-07 18:03

    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

    0 讨论(0)
  • 2020-12-07 18:06

    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>
    
    0 讨论(0)
  • 2020-12-07 18:07

    keep log4j file in default package

    0 讨论(0)
  • 2020-12-07 18:07

    I like the logback way, and for slf4j, we do the similar config:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
    

    slf4j-log4j12 will automatically introduce slf4j-api and log4j, so don't need to put so many dependencies

    0 讨论(0)
  • 2020-12-07 18:16

    Just add lazy-init="false" to eagerly load the bean for log4j configuration in your root context. That should solve the WARN message log4j:WARN No appenders could be found for logger

    Example:

    <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
    

    A more better approach would be to have the configuration in web.xml or as a JVM parameter (-Dlog4j.configuration=.../conf/log4j.xml or with 'file:' prefix as -Dlog4j.configuration=file:conf/log4j.properties for some cases)

    0 讨论(0)
提交回复
热议问题