新日志框架LogBack介绍
1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等
2、logback介绍:基于Log4j基础上大量改良,不能单独使用,推荐配合日志框架SLF4J来使用
logback当前分成三个模块:logback-core,logback-classic和logback-access;
logback-core是其它两个模块的基础模块
3、Logback的核心对象:
Logger:日志记录器
Appender:指定日志输出的目的地,目的地可以是控制台,文件
Layout:日志布局 格式化日志信息的输出
4、日志级别:DEBUG < INFO < WARN < ERROR
log4j配置===========log4j示例===========
设置
log4j.rootLogger = debug,stdout,D,E
输出信息到控制抬
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
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
输出DEBUG 级别以上的日志到=D://logs/error.log
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
输出ERROR 级别以上的日志到=D://logs/error.log
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
SpringBoot2.x日志讲解和自定义Logback配置实战
分析SpringBoot启动日志
默认情况下,Spring Boot将日志输出到控制台
整合Logback实战
-创建 日志文件logback-spring.xml,官方推荐 -spring.xml结尾
-默认加载加载配置顺序 logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
注释:
<configuration> 子节点
<appender></appender>
<logger></logger>
<root></root>(要加在最后)
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="consoleApp" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</layout>
</appender>
<appender name="fileInfoApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="fileErrorApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 设置滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.err.%d.log</fileNamePattern>
<!-- 控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动, 且<maxHistory> 是1,则只保存最近1个月的文件,删除之前的旧文件 -->
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="consoleApp" />
<appender-ref ref="fileInfoApp" />
<appender-ref ref="fileErrorApp" />
</root>
</configuration>
文件位置位于resource目录下
编写controller 测试
package vip.buildup.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//封装的消息返回
import vip.buildup.demo.domain.JsonData;
@RestController
@RequestMapping("/api/v2")
public class TestLogBackController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@GetMapping("test_log")
public Object testLog() {
logger.debug("this is debug level");
logger.info("this is debug level");
logger.warn("this is debug level");
logger.error("this is debug level");
return JsonData.buildSuccess();
}
}
测试接口后,刷新项目,项目路径下 生成app_log目录,目录下生成日志文件。