分享你我的心得.
共乘一片美好网络.

报错: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

报错:
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
表示sl4j和log4j的不兼容,
maven导入两个依赖即可:

<!– https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>

<!– https://mvnrepository.com/artifact/log4j/log4j –>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

具体详解参考

https://blog.csdn.net/langtian08/article/details/82015017

接下来能会报

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

这样的错误

首选你的

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

这两行不报错

否则你需要加入slf4j-api-1.7.2.jar包

接下来解决报错问题

log4j没有配置日志记录的位置,需要配置log4j.properties,在src目录main目录resources文件夹下下新建log4j.properties

可以将下面的内容写入文件

1.应用于控制台

log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.Threshold = DEBUG
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

#log4j.appender.CONSOLE.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] n % c[CATEGORY] % n % m[MESSAGE] % n % n

2.应用于文件
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File = file.log
log4j.appender.FILE.Append = false
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

# Use this layout for LogFactor 5 analysis

3.应用于文件
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File = file.log
log4j.appender.FILE.Append = false
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [framework] % d – % c -%- 4r [ % t] %- 5p % c % x – % m % n

在这里我的配置文件为:

log4j.rootLogger=warn,CONSOLE,File

#Console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} [%t] [%c] [%p] – %m%n

#File DailyRollingFileAppender
log4j.logger.File=info
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} [%t] [%c] [%p] – %m%n
log4j.appender.File.datePattern=’.’yyyy-MM-dd
log4j.appender.File.Threshold = info
log4j.appender.File.append=true
log4j.appender.File.File=d://code/logs/flink/disk.log

配置完成后重新打包即可

注意查看打包后是否在target—classes文件下,如果不在就无法解决,此时需要手动复制进去

这是打包后不在的情形

复制到target—–classes文件中

此时再次执行成功

 

赞(0)
未经允许不得转载:小叶白龙博客 » 报错: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!