- 在pom文件中屏蔽掉默认的日志方式spring-boot-starter-logging,并引入log4j相关依赖(如果其他依赖也使用了跟log4j不相容的也排除掉即可):
<!-- log4j start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<!-- log4j end -->
-
项目目录下创建res文件夹,创建log4j.properties文件,你也可以放到src/main/resources下面,此时不需要再加如下的语句 PropertyConfigurator.configure(System.getProperty("logging.config", "res/log4j.properties"));
使用res目录是考虑到不同的环境可能需要不同的日志级别,只需要修改res目录下的配置即可。在启动类中这样设置:
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.cdv.ipdm")
@MapperScan(basePackages = "com.cdv.ipdm.dao.mapper")
@EnableScheduling
@EnableAsync
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
PropertyConfigurator.configure(System.getProperty("logging.config", "res/log4j.properties"));
SpringApplication.run(Application.class, args);
}
}
- 在log4j.properties中加入如下内容
log4j.rootLogger=INFO,err,stdout
#数据库访问mapper接口所在包日志级别(自行修改)
log4j.logger.com.cdv.ipdm.common.mapper=DEBUG,db
#db.log,每天一个文件
log4j.logger.db=DEBUG,db
log4j.appender.db=org.apache.log4j.DailyRollingFileAppender
log4j.appender.db.Threshold=DEBUG
log4j.appender.db.File=./logs/db.log
log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.appender.db.layout.ConversionPattern=%5p %d{yyyy-MM-dd HH\:mm\:ss} [%F\:%L] %m %n
|