Re: ERROR SLF4J: Class path contains multiple SLF4J bindings Neeraj Upadhyay Jan 2, 2020 7:06 AM ( in response to yogendra tirupattur ) Please note this is a generic warning message and you can ignore the same if it is not making your task to fail.there is an open bug for this issue: CCON-18220. Problem description: 'Class path contains multiple SLF4J bindings' message appears in WebLogic 10.3 log during startup of an application with SLF4J 1.5.8. The problem is that exactly the same binding is printed twice and therefore the warning is irrelevant: SLF4J: Found binding in. SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Class path contains multiple SLF4J bindings with maven Tag: java, spring, maven, slf4j, logback I have created cutom logging in my application using - SLF4J, following the BELOW Link. SLF4J: Class path contains multiple SLF4J bindings. Preciso do vosso apoio.
- Status:Closed
- Resolution: Not a bug
- Fix Version/s: None
- Labels:
- Environment:
Estou fazendo um programa em java usando Maven 3.6.3 JSF 2.2 e Jboos Junit 4.12 e hibernate
o meu programa rooda bem, só que ao usar o teste do Maven clean ele da este erro:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Desenvolvimento/IDE/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in file:/C:/Desenvolvimento/IDE/eclipse/configuration/org.eclipse.osgi/5/0/.cp/org/slf4j/impl/StaticLoggerBinder.class
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
Preciso do vosso apoio.
Derito
- Assignee:
- SLF4J developers list
- Reporter:
- Hélder Costa
- Votes:
- 0Vote for this issue
- Watchers:
- 1Start watching this issue
- SLF4J Tutorial
- SLF4J Useful Resources
- Selected Reading
SLF4J stands for Simple Logging Facade for Java. It provides a simple abstraction of all the logging frameworks in Java. Thus, it enables a user to work with any of the logging frameworks such as Log4j, Logback and JUL (java.util.logging) using single dependency. You can migrate to the required logging framework at run-time/deployment time.
Ceki Gülcü created SLF4J as an alternative to Jakarta commons-logging framework.
Advantages of SLF4J
Following are the advantages of SLF4J −
Using SLF4J framework, you can migrate to the desired logging framework at the time of deployment.
Slf4J provides bindings to all popular logging frameworks such as log4j, JUL, Simple logging and, NOP. Therefore, you can switch to any of these popular frameworks at the time of deployment.
SLF4J provides support to parameterized logging messages irrespective of the binding you use.
Since SLF4J decouples application and logging framework, you can easily write applications independent of logging frameworks. You need not bother about the logging framework being used to write an application.
SLF4J provides a simple Java tool known as migrator. Using this tool, you can migrate existing projects, which use logging frame works like Jakarta Commons Logging (JCL) or, log4j or, Java.util.logging (JUL) to SLF4J.
Logging in programming, refers to recording activities/events. Usually, the application developers should take care of logging.
To make the job of logging easier, Java provides various frameworks − log4J, java.util.logging (JUL), tiny log, logback, etc.
Logging Framework Overview
A logging framework usually contains three elements −
Logger
Captures the message along with the metadata.
Formatter
Formats the messages captured by the logger.
Handler
The Handler or appender finally dispatches the messages either by printing on the console or, by storing in the database or, by sending through an email.
Some frameworks combine the logger and appender elements to speed up the operations.
Logger Object
To log a message, the application sends a logger object (sometimes along with the exceptions if any) with name and security level.
Severity Level
The messages logged will be of various levels. The following table lists down the general levels of logging.
Sr.No | Severity level & Description |
---|---|
1 | Fatal Severe issue that causes the application to terminate. |
2 | ERROR Runtime errors. |
3 | WARNING In most cases, the errors are due to the usage of deprecated APIs. |
4 | INFO Events that occur at runtime. |
5 | DEBUG Information about the flow of the system. |
6 | TRACE More detailed information about the flow of the system. |
What is log4j?
log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License.
log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog, etc. (for more details on log4j refer our Tutorial).
Comparison SLF4J and Log4j
Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.
If you have a choice, logging abstraction is always preferable than logging framework. If you use a logging abstraction, SLF4J in particular, you can migrate to any logging framework you need at the time of deployment without opting for single dependency.
Observe the following diagram to have a better understanding.
In this chapter, we will explain how to set SLF4J environment in Eclipse IDE. Before proceeding with the installation, make sure that you already have Eclipse installed in your system. If not, download and install Eclipse.
For more information on Eclipse, please refer our Eclipse Tutorial
Step 1: Download the dependency JAR file
Open the official homepage of the SLF4J website and go to the download page.
Now, download the latest stable version of slf4j-X.X.tar.gz or slf4j-X.X.zip, according to your operating system (if windows .zip file or if Linux tar.gz file).
Within the downloaded folder, you will find slf4j-api-X.X.jar. This is the required Jar file.
Step 2: Create a project and set build path
Open eclipse and create a sample project. Right-click on the project, select the option Build Path → Configure Build Path… as shown below.
In the Java Build Path frame in the Libraries tab, click Add External JARs…
Select the slf4j-api.x.x.jar file downloaded and click Apply and Close.
SLF4J Bindings
In addition to slf4j-api.x.x.jar file, SLF4J provides several other Jar files as shown below. These are called SLF4J bindings.
Where each binding is for its respective logging framework.
The following table lists the SLF4J bindings and their corresponding frameworks.
Sr.No | Jar file & Logging Framework |
---|---|
1 | slf4j-nop-x.x.jar No operation, discards all loggings. |
2 | slf4j-simple-x.x.jar Simple implementation where messages for info and higher are printed and, remaining all outputs to System.err. |
3 | slf4j-jcl-x.x.jar Jakarta Commons Logging framework. |
4 | slf4j-jdk14-x.x.jar Java.util.logging framework (JUL). |
5 | slf4j-log4j12-x.x.jar Log4J frame work. In addition, you need to have log4j.jar. |
To make SLF4J work along with slf4l-api-x.x.jar, you need to add the respective Jar file (binding) of the desired logger framework in the classpath of the project (set build path).
To switch from one framework to other, you need to replace the respective binding. If no bounding is found, it defaults to no-operation mode.
Pom.xml for SLF4J
If you are creating the maven project, open the pom.xml and paste the following content in it and refresh the project.
In this chapter, we will discuss the classes and methods that we will be using in the subsequent chapters of this tutorial.
Logger Interface
The logger interface of the org.slf4j package is the entry point of the SLF4J API. The following lists down the important methods of this interface.
Sr.No. | Methods and Description |
---|---|
1 | void debug(String msg) This method logs a message at the DEBUG level. |
2 | void error(String msg) This method logs a message at the ERROR level. |
3 | void info(String msg) This method logs a message at the INFO level. |
4 | void trace(String msg) This method logs a message at the TRACE level. |
5 | void warn(String msg) This method logs a message at the WARN level. |
LoggerFactory Class
The LoggerFactory class of the org.slf4j package is a utility class, which is used to generate loggers for various logging APIs such as log4j, JUL, NOP and simple logger.
Sr.No. | Method and Description |
---|---|
1 | Logger getLogger(String name) This method accepts a string value representing a name and returns a Logger object with the specified name. |
Profiler Class
This class belongs to the package org.slf4j this is used for profiling purpose and it is known as poor man’s profiler. Using this, the programmer can find out the time taken to carry out prolonged tasks.
Following are the important methods of this class.
Sr.No. | Methods and Description |
---|---|
1 | void start(String name) This method will start a new child stop watch (named) and, stops the earlier child stopwatches (or, time instruments). |
2 | TimeInstrument stop() This method will stop the recent child stopwatch and the global stopwatch and return the current Time Instrument. |
3 | void setLogger(Logger logger) This method accepts a Logger object and associates the specified logger to the current Profiler. |
4 | void log() Logs the contents of the current time instrument that is associated with a logger. |
5 | void print() Prints the contents of the current time instrument. |
In this chapter, we will see a simple basic logger program using SLF4J. Follow the steps described below to write a simple logger.
Step 1 - Create an object of the slf4j.Logger interface
Since the slf4j.Logger is the entry point of the SLF4J API, first, you need to get/create its object
The getLogger() method of the LoggerFactory class accepts a string value representing a name and returns a Logger object with the specified name.
Step 2 - Log the required message
The info() method of the slf4j.Logger interface accepts a string value representing the required message and logs it at the info level.
Example
Following is the program that demonstrates how to write a sample logger in Java using SLF4J.
Output
On running the following program initially, you will get the following output instead of the desired message.
Since we have not set the classpath to any binding representing a logging framework, as mentioned earlier in this tutorial, SLF4J defaulted to a no-operation implementation. So, to see the message you need to add the desired binding in the project classpath. Since we are using eclipse, set build path for respective JAR file or, add its dependency in the pom.xml file.
For example, if we need to use JUL (Java.util.logging framework), we need to set build path for the jar file slf4j-jdk14-x.x.jar. And if we want to use log4J logging framework, we need to set build path or, add dependencies for the jar files slf4j-log4j12-x.x.jar and log4j.jar.
After adding the binding representing any of the logging frameworks except slf4j-nopx.x.jar to the project (classpath), you will get the following output.
In this chapter, we will discuss the various error messages or warning we get while working with SLF4J and the causes/ meanings of those messages.
Failed to load class 'org.slf4j.impl.StaticLoggerBinder'.
This is a warning which is caused when there are no SLF4J bindings provided in the classpath.
Following is the complete warning −
Logback 1.2.3
To resolve this, you need to add either of the logging framework bindings. This is explained in the Hello world chapter of this tutorial.
Note − This occurs in versions of SLF4J which are between 1.6.0 and 1.8.0-beta2.
No SLF4J providers were found
In slf4j-1.8.0-beta2, the above warning is more clear saying “No SLF4J providers were found”.
Following is the complete warning −
Classpath contains SLF4J bindings targeting slf4j-api versions prior to 1.8
If you are using SLF4J 1.8 version and you have the bindings of previous versions in the classpath but not the bindings of 1.8 you will see a warning as shown below.
NoClassDefFoundError: org/apache/commons/logging/LogFactory
If you are working with slf4j-jcl and if you have only slf4j-jcl.jar in your classpath, you will get an exception such as the one given below.
To resolve this, you need to add commons-logging.jar to your classpath.
Detected both jcl-over-slf4j.jar AND bound slf4j-jcl.jar on the classpath..
The binding slf4j-jcl.jar redirects calls of the slf4j logger to JCL and the jcl-over-slf4j.jar redirects calls of JCL logger to slf4j. Therefore, you cannot have both in the classpath of your project. If you do so, you will get an exception such as the one given below.
To resolve this, delete either of the jar files.
Detected logger name mismatch
You can create a Logger object by −
Passing the name of the logger to be created as an argument to the getLogger() method.
Passing a class as an argument to this method.
If you are trying to create the logger factory object by passing a class as an argument, and if you have set the system property slf4j.detectLoggerNameMismatch to true, then the name of the class you pass as an argument to the getLogger() method and the class you use should be the same otherwise you will receive the following warning −
“Detected logger name mismatch.
Consider the following example.
Here, we have set the slf4j.detectLoggerNameMismatch property to true. The name of the class we used is SLF4JExample and the class name we have passed to the getLogger() method is Sample since they both are not equal we will get the following warning.
Note − This occurs after slf4j 1.7.9
Classpath contains multiple SLF4J bindings.
You should have only one binding in the classpath. If you have more than one binding, you will get a warning listing the bindings and the locations of them.
For suppose, if we have the bindings slf4j-jdk14.jar and slf4j-nop.jar in the classpath we will get the following warning.
Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path
To redirect the log4j logger calls to slf4j, you need to use log4j-over-slf4j.jar binding and if you want to redirect slf4j calls to log4j, you need to use slf4j-log4j12.jar binding.
Therefore, you cannot have both in the classpath. If you do, you will get the following exception.
As discussed earlier in this tutorial SLF4J provides support for parameterized log messages.
You can use parameters in the messages and pass values to them later in the same statement.
Syntax
As shown below, you need to use placeholders ({}) in the message (String) wherever you need and later you can pass value for place holder in object form, separating the message and value with comma.
Example
The following example demonstrates parameterized logging (with single parameter) using SLF4J.
Output
Upon execution, the above program generates the following output −
Advantage of Parameterized Logging
In Java, if we need to print values in a statement, we will use concatenation operator as −
This involves the conversion of the integer value 23 to string and concatenation of this value to the strings surrounding it.
And if it is a logging statement, and if that particular log level of your statement is disabled then, all this calculation will be of no use.
In such circumstances, you can use parameterized logging. In this format, initially SLF4J confirms whether the logging for particular level is enabled. If so then, it replaces the placeholders in the messages with the respective values.
For example, if we have a statement as
Only if debugging is enabled then, SLF4J converts the age into integer and concatenates it with the strings otherwise, it does nothing. Thus incurring the cost of parameter constructions when logging level is disabled.
Two Argument Variant
You can also use two parameters in a message as −
Example
The following example demonstrates the usage of two placeholders in parametrized logging.
Output
Upon execution, the above program generates the following output.
Multiple Argument Variant
You can also use more than two placeholders as shown in the following example −
Output
Upon execution, the above program generates the following output −
If you have a project in Jakarta Commons Logging (JCL) or, log4j or, java.util.logging (JUL) and you want to convert these projects to SLF4J, you can do so using the migrator tool provided in the SLF4J distribution.
Running SLF4J Migrator
SLF4J is a simple single jar file (slf4j-migrator.jar) and you can run it using the java –jar command.
To run it, in command prompt, browse through the directory where you have this jar file and execute the following command.
This starts the migrator and you can see a standalone java application as −
As specified in the window, you need to check the type of migration you want to do and select the project directory and click on the button Migrate Project to SLF4J.
This tool goes to the source files you provide and performs simple modifications like changing the import lines and logger declarations from the current logging framework to SLF4j.
Example
For example, let us suppose we have a sample log4j(2) project in eclipse with a single file as follows −
To migrate the sample log4j(2) project to slf4j, we need to check the radio button from log4j to slf4j and select the directory of the project and click Exit to migrate.
The migrator changed the above code as follows. Here if you observe the import and logger statements have been modified.
Since you already have log4j.jar in your project, you need to add slf4j-api.jar and slf4jlog12.jar files to the project to execute it.
Limitations of SLF4JMigrator
Following are the limitations of the SLF4J migrator.
Migrator will not modify build scripts like ant, maven and, ivy you need to do it yourself.
Migrator does not support messages other than the String type.
Migrator does not support the FATAL level.
While working with log4j, migrator will not migrate calls to PropertyConfigurator or DomConfigurator.
SLF4J Distribution provides slf4j-ext.jar this contains APIs for the functionalities such as profiling, Extended logging, Event logging and, logging with java agent.
Profiling
Sometimes the programmer wants to measure some attributes like the use of memory, time complexity or usage of particular instructions about the programs to measure the real capability of that program. Such kind of measuring about the program is called profiling. Profiling uses dynamic program analysis to do such measuring.
SLF4J provides a class named Profiler in the org.slf4j.profiler package for profiling purpose. This is known as the poor man’s profiler. Using this, the programmer can find out the time taken to carry out prolonged tasks.
Profiling Using the Profiler class
The profiler contains stopwatches and child stopwatches and we can start and stop these using the methods provided by the profiler class.
To carry on with profiling using the profiler class, follow the steps given below.
Step 1 - Instantiate the profiler class
Instantiate the Profiler class by passing a String value representing the name of the profiler. When we instantiate a Profiler class, a global stopwatch will be started.
Lf4j: Class Path Contains Multiple Slf4j Bindings
Step 2 - Start a child stopwatch
When we invoke the start() method it will start a new child stopwatch (named) and, stops the earlier child stopwatches (or, time instruments).
Invoke the start() method of the Profiler class by passing a String value representing the name of the child stopwatch to be created.
After creating these stopwatches, you can perform your tasks or, invoke those methods, which run your tasks.
Step 3: Start another child stopwatch (if you wish to)
If you need, create another stopwatch using the start() method and perform the required tasks. If you do so, it will start a new stop watch and stops the previous one (i.e. task 1).
Step 4: Stop the watches
When we invoke the stop() method, it will stop the recent child stopwatch and the global stopwatch and returns the current Time Instrument.
Step 5: Print the contents of the time instrument.
Print the contents of the current time instrument using the print() method.
Example
The following example demonstrates the profiling using Profiler class of SLF4J. Here we have taken two sample tasks, printing the sum of squares of the numbers from 1 to 10000, printing the sum of the numbers from 1 to 10000. We are trying to get the time taken for these two tasks.
Output
Upon execution, the above program generates the following output −
Logging the Profiler Information
Instead of printing the result of a profiler to log this information, you need to −
Create a logger using the LoggerFactory class.
Create a profiler by instantiating the Profiler class.
Associate the logger to profiler by passing the logger object created to the setLogger() method of the Profiler class.
Finally, instead of printing log the information of the profiler using the log() method.
Example
In the following example, unlike the previous one (instead of printing), we are trying to log the contents of the time instrument.
Output
Upon execution, the above program generates the following output.