【问题标题】:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" error when using log4jSLF4J:使用 log4j 时无法加载类“org.slf4j.impl.StaticLoggerBinder”错误
【发布时间】:2020-03-20 08:09:30
【问题描述】:

我在我的 maven 项目中使用 log4j 依赖项。但是我收到了与 slf4j 相关的警告。但是我的 pom 中只有 log4j 依赖项。

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>compile</scope>
    </dependency>

我查看了http://www.slf4j.org/codes.html#StaticLoggerBinder 并尝试添加

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.29</version>
    <scope>test</scope>
</dependency>

到代码。但它没有用。有什么解释吗?

【问题讨论】:

  • 你可能想要groupId org.apache.logging.log4j,这个是2012年的
  • @boot-and-bonnet 它说当我更改 groupId 时没有这样的依赖
  • 选项 1: 要摆脱错误,只需更改 slf4j-log4j12 依赖范围即可编译。 选项 2: 如果您只想使用 log4j,请确保您的其他依赖项没有拉 slf4j-api jar(即传递依赖项)。
  • @Ramu 如果依赖项传递地需要 SLF4J,那么你不能不把它拉进去;这将导致在执行期间至少抛出一个NoClassDefFoundError

标签: java maven log4j slf4j


【解决方案1】:

正如您所说,SLf4J 网站提供了解决此问题的解决方案。按照此操作并将slf4j-simple-1.7.29.jarslf4j-api-1.7.29.jar 添加到您的应用程序中。

SLF4J 简单绑定 » 1.7.29

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.29</version>
    <scope>test</scope>
</dependency>

SLF4J API 模块 » 1.7.29

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.29</version>
</dependency>

希望这对您和遇到此问题的其他人有所帮助。

【讨论】:

    【解决方案2】:
                    <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.7</version>
    
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.7</version>
    
            </dependency>
    
                    <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.6</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.5</version>
            </dependency>
    
    You log4j.properties will be 
    
    # Root logger option
    #log4j.rootLogger=DEBUG, stdout, file
    log4j.logger.com.dnd.ncprdatacouch=DEBUG, stdout, file
    
    # Redirect log messages to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    
    you should use following
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    private static final Logger logger = LoggerFactory.getLogger(NcprCouchFullDump.class);
    
    # Redirect log messages to a log file, support file rolling.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    #log4j.appender.file.File=C:/vivek/go/logs/log4j-application.log
    log4j.appender.file.File=<your Path>/log4j-application.log
    log4j.appender.file.MaxFileSize=30MB
    log4j.appender.file.MaxBackupIndex=50
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2011-11-17
      • 2017-07-24
      • 2021-05-08
      • 1970-01-01
      相关资源
      最近更新 更多