I'm getting the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
I need to include slf4j as a dependency but I'm not sure which jar file to download from https://repo1.maven.org/maven2/org/slf4j/.
I tried org/slf4j/slf4j-ap
i which doesn't work. How do I determine which one is correct?
CodePudding user response:
slf4j
is a library that is used as an abstraction of each logging implementation framework such as log4j
, logback
or Jakarta Commons Logging
. If your intent is to use it, you have to also define the concrete framework you want to use under the wood, for example for log4j
, your pom
dependencies will look like this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Depending on the framework you want to use, the dependencies will be a bit different.
This excellent link will give your more details.
CodePudding user response:
SLF4J is a logging API. Combine it with Logback (written by the same people as SLF4J) which implements that logging API:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
Find the latest stable versions on mvnrepository.com. Currently logback 1.2.11 and slf4J 1.7.32. Note the the vulnerabilities mentioned on logback 1.2.11 are because of an optional dependency on log4J 1.x (which you don't get automatically, so it's safe to use logback 1.2.11 at the time of writing).