0
votes

Each spring mvc project library uses its own logging framework viz. log4j, slf4j, logback, jboss-logging, commons-logging etc. as given below with maven integration.

pom.xml

<log4j.version>1.6.5</log4j.version>
<slf4j.version>1.7.16</slf4j.version>
<slf4j.log4j13.version>1.0.1</slf4j.log4j13.version>
<logback.version>1.1.2</logback.version>
<jboss.logging.version>3.3.0.Final</jboss.logging.version>
<commons.logging.version>1.2</commons.logging.version>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-bom</artifactId>
      <version>2.5</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
<dependency>
    <groupId>ant</groupId>
    <artifactId>ant-jakarta-log4j</artifactId>
    <version>1.6.1</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j13</artifactId>
    <version>${slf4j.log4j13.version}</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>  
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
</dependency>
</dependencies>

Here is the Severe Error displayed while running the project

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/WEB-INF/lib/log4j-slf4j-impl-2.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/WEB-INF/lib/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/WEB-INF/lib/slf4j-log4j13-1.0.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Each logging framework is needed for the project to be included. But is there any trick to retain the logging framewokrs needed by the external libraries while running the project without errors.

1

1 Answers

1
votes

Typically you will have multiple components that each use different logging APIs. What you normally want to do is bind each of those with a specific logging implementation. For example, Spring uses commons-logging so to route it to Log4j 2 you would include the log4j-jcl jar. Likewise, to route SLF4J to Log4j 2 you would include the log4j-slf4j-impl jar. You would not include any Logback jars since it is another logging implementation. In the case above the errors you are getting show that you have the Log4j 2 SLF4J binding, logback, log4j1.2 binding and log4j 1.3 bindings for SLF4J. You should only have 1 of them, so remove the jars for the ones you don't want to use.

Note that SLF4J is telling you that it chose the Log4j 2 binding but then you are getting an error from Log4j 2 informing you that it can't find a configuration file - typically this would be log4j2.xml.