Reputation: 899
Using slf4j-log4j12 version- 2.0.0-alpha5 dependency for logging which pulls Apache Log4j » 1.2.17. Need to upgrade the Log4j to latest which is log4j-core » 2.17.1 such that I don't have to make changes in the codebase.
Below is the snippet of POM and sample code:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha5</version>
</dependency>
</dependencies>
Sample Code:
import org.apache.log4j.Logger;
public class Entry {
final static Logger logger = Logger.getLogger(Entry.class);
public static void main(String[] args) {
logger.info("Through Logging");
System.out.println("Logging tests");
}
}
Is there any sl4j dependency for log4j2?
Upvotes: 5
Views: 17243
Reputation: 16045
The slf4j-log4j12
is a bridge (binding) from SLF4J to Log4j 1.2: all messages submitted to a org.slf4j.Logger
in your code, will be sent to a org.apache.log4j.Logger
of the same name. Its not the right direction.
Fortunately nowadays there are bridges/bindings/adapters between all major logging systems. It is not clear from your question, whether you need SLF4J at all, so:
if you don't use SLF4J, you can use log4j-1.2-api
, a direct bridge between Log4j 1.x and Log4j 2.x API. It is a replacement for Log4j 1.x, so you need to remove the log4j
artifact and add:
<!-- From Log4j 1.x to Log4j 2.x API -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.19.0</version>
</dependency>
if you want to pass through SLF4J, you can use log4j-over-slf4j
(a replacement for Log4j 1.x that connects Log4j 1.x to SLF4J) and log4j-slf4j18-impl
(a bridge from SLF4J to the Log4j 2.x API):
<!-- From Log4j 1.x to SLF4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>2.0.5</version>
</dependency>
<!-- From SLF4J 2.0+ to Log4j 2.x API -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.19.0</version>
<scope>runtime</scope>
</dependency>
Of course you'll also need an implementation of the Log4j 2.x API, like Log4j 2.x Core:
<!-- From Log4j 2.x API to Log4j 2.x Core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
<scope>runtime</scope>
</dependency>
Upvotes: 8
Reputation: 42615
You can try to exclude the log4 1.2.17 dependency and instead use log4j2-core. The following configuration is untested, not sure if it works:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha5</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
Upvotes: 1