Getting Exception org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

后端 未结 2 982
不思量自难忘°
不思量自难忘° 2020-12-05 18:06

My code is very simple using apache-log4j-2.0.2:

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Log4jtest {
  stati         


        
2条回答
  •  广开言路
    2020-12-05 18:24

    I was using Maven. I found that declaring my log4j/slf4j dependencies at the top of the list (before Spring Boot, which used logback) fixed the issue.


    To @TheCodingFrog's credit, adding exclusions to my Spring Boot dependencies also solved the problem. Like so:

    
        
            ...
            ...
            
                
                    log4j
                    *
                
                
                    org.slf4j
                    *
                
                
                    org.apache.logging.log4j
                    *
                
            
        
    
    

    NOTE: If you care about which logging framework is actually used, one perhaps important difference is that with @TheCodingFrog's method, slf4j retained logback as the binding:

    SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
    

    whereas, with the method I used, slf4j/log4j was used:

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

    In case anyone's interested, the log4j/slf4j dependencies I used are:

    
        log4j
        log4j
        1.2.17
    
    
        org.slf4j
        slf4j-api
        1.7.7
    
    
        org.slf4j
        slf4j-log4j12
        1.7.7
    
    

提交回复
热议问题