SLF4J:使用log4j时无法加载类“ org.slf4j.impl.StaticLoggerBinder”错误

时间:2019-11-25 03:42:58

标签: java maven log4j slf4j

我在我的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 adnd尝试添加

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

到代码。但这没有用。有任何澄清吗?

2 个答案:

答案 0 :(得分:0)

正如您所说,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>

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

答案 1 :(得分:0)

                <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