Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using log4j2 with slf4j: java.lang.StackOverflowError

Tags:

java

slf4j

log4j2

So I have tried following this (non-maven implementation) and requirements in their web site for adding slf4j to log4j. and tried using this code

public static void main(String[] args) {         Logger logger = LoggerFactory.getLogger(Main.class);         logger.info("test");     } 

and added the following to my library

  • log4j-api-2.3.jar
  • log4j-core-2.3.jar
  • log4j-sl4j-impl-2.3.jar
  • log4j-to-sl4j-2.3.jar
  • slf4j-api-1.7.12.jar

when I try running it I get the following error

Exception in thread "main" java.lang.StackOverflowError     at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)     at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)     at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:40)     at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)     at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)     at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)     at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:41)     at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)     at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)     at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)     at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)... 

Any idea where I am going wrong?

like image 834
Akshay Avatar asked Sep 03 '15 03:09

Akshay


People also ask

How does SLF4J work with log4j?

SLF4J standardized the logging levels, which are different for the particular implementations. It drops the FATAL logging level (introduced in Log4j) based on the premise that in a logging framework we should not decide when to terminate an application. The logging levels used are ERROR, WARN, INFO, DEBUG and TRACE.

Can SLF4J work without log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.

Does SLF4J use Apache log4j?

SLF4J ship with a module called log4j-over-slf4j. It allows log4j users to migrate existing applications to SLF4J without changing a single line of code but simply by replacing the log4j. jar file with log4j-over-slf4j. jar, as described below.


1 Answers

You're creating a call-loop with log4j-slf4j-impl-2.3.jar and log4j-to-slf4j-2.3.jar.

log4j-slf4j-impl-2.3.jar is the implementation of the adapter that sends slf4j calls to log4j.

log4j-to-slf4j-2.3.jar is sending log4j calls right back to slf4j. Remove this one.

like image 137
Andreas Avatar answered Sep 25 '22 12:09

Andreas