SLF4J常见问题
导入依赖:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.15</version>
</dependency>
基本使用:
package com.cc;
import org.apache.log4j.Logger;
public class Test01 {
private static final Logger logger = Logger.getLogger(Test01.class);
public static void main(String[] args) {
logger.info("This is a test.");
}
}
错误信息1:
在resources
下新建log4j.properties
日志配置文件:
### 配置根 ###
# 控制将指定级别(INFO)的日志输出到哪些目的地(CONSOLE,fileAppender,dailyRollingFile,ROLLING_FILE)
#log4j.rootLogger=INFO,CONSOLE,fileAppender,dailyRollingFile,ROLLING_FILE
log4j.rootCategory=INFO,console
### 配置输出到控制台 ###
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy/MM/dd HH:mm:ss} [%-5p] %c{1}:%L: %m%n
### 配置输出到文件 ###
# 输出DEBUG级别以上的日志到logs/error.log
log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.File=logs/error.log
log4j.appender.fileAppender.Append=true
log4j.appender.fileAppender.Threshold=DEBUG
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 配置输出到文件,并且每天都创建一个文件 ###
# 输出DEBUG级别以上的日志到logs/error.log
log4j.appender.dailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRollingFile.File=logs/error.log
log4j.appender.dailyRollingFile.Append=true
log4j.appender.dailyRollingFile.Threshold=DEBUG
log4j.appender.dailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyRollingFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 配置输出到文件,且到达指定大小的时候产生一个新的文件 ###
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
错误信息2:
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.
根据官网描述:
只需要将slf4j-nop.jar
、slf4j-simple.jar
、slf4j-log4j12.jar
、slf4j-jdk14.jar
或logback-classic.jar
中的一个(且只能一个)添加到类路径即可
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
参考文章:
https://cloud.tencent.com/developer/article/2160396
https://www.modb.pro/db/130472