在pom文件中添加依赖包
<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.25</slf4j.version>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
在resources目录下创建 log4j.properties 配置文件
### 设置###
log4j.rootLogger=INFO,stdout,file
log4j.logger.org.apache=WARN
log4j.logger.org.quartz=INFO
log4j.logger.com.mchange=WARN
log4j.logger.com.mchange.v2=off
log4j.logger.com.sophon.ontology=off
### 输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出到文件##
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=./log/develop.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] - [ %F:%M:%L ] - %m%n
代码中使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(AppEntry.class);