SpringBoot学习笔记:整合sl4j+log4j
Posted 听风者-better
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习笔记:整合sl4j+log4j相关的知识,希望对你有一定的参考价值。
1.添加log4j依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除自带的logback依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Boot log4j依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
2.创建log4j.properties文件
#配置日志根Logger
log4j.rootLogger=INFO,stdout,file,errorLog
#输出到控制台的配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss %-5p %c1:%L - %m%n
#输出到文件的配置
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.file.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss,SSS %5p %c1:%L - %m%n
log4j.appender.file.file=/logs/springboot.log
# ERROR级别输出到特定的日志文件中
log4j.logger.error=errorLog
# error日志输出
log4j.appender.errorLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorLog.file=/logs/springboot-error.log
log4j.appender.errorLog.DatePattern='.'yyyy-MM-dd
log4j.appender.errorLog.Threshold = ERROR
log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss,SSS %5p %c1:%L - %m%n
3.创建测试方法
package com.twy.springbootsl4j.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName TestController
* @Description
* @author twy
* @date 2020-05-11 22:14
*/
@RestController
public class TestController
private Logger logger = LoggerFactory.getLogger(TestController.class);
@GetMapping("/test")
public void test()
logger.info("sl4j info");
logger.warn("sl4j warn");
logger.debug("sl4j debug");
logger.error("sl4j error");
4.测试结果
生成对应的日志文件:
全部日志文件:
错误日志文件:
以上是关于SpringBoot学习笔记:整合sl4j+log4j的主要内容,如果未能解决你的问题,请参考以下文章
springboot学习笔记-5 springboot整合shiro
springboot学习笔记-5 springboot整合shiro