- 添加依赖
- 将logback.xml添加到resources目录(否则无法加载)
- 使用方式
- 配置文件详细信息
- 配置文件参数配置解析====>logback.xml日志内容解析
1.添加依赖
因为springboot已经对logback做了兼容,不需要再导入别的spring相关的logback包
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
2.将logback.xml添加到resources目录(否则无法加载)
3.使用方式
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {
private static transient Logger log = LoggerFactory.getLogger(UserController.class);
@RequestMapping("/userList")
@ResponseBody
public WebJsonBean getUserList(){
List<User> list = new ArrayList<>();
User u = new User();
u.setAddress("ShenZhen");
u.setAge(21);
u.setName("zhangsan");
list.add(u);
log.info("result {}", JSON.toJSONString(list));
return new WebJsonBean(list);
}
}
4.配置文件详细信息
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.base" value="D:/data/logs/"/> <!-- 日志根目录 -->
<property name="log.moduleName" value="springboot1"/> <!-- 模块名称, 影响日志配置名,日志文件名 -->
<property name="log.max.size" value="10MB"/> <!-- 日志文件大小 -->
<!--控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
<![CDATA[%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%.6thread]%logger{36}.%method<%L> %msg%n]]></Pattern>
</encoder>
</appender>
<!-- 日志文件输出 -->
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.base}/${log.moduleName}/${log.moduleName}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}/${log.moduleName}/archive/${log.moduleName}_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<!-- 当天的日志大小 超过${log.max.size}时,压缩日志并保存 -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<!-- 日志输出的文件的格式 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
<![CDATA[%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%.6thread]%logger{36}.%method<%L> %msg%n]]></pattern>
</layout>
</appender>
<root level="debug"><!--日志等级debug,info,warn,error,fail-->
<appender-ref ref="file"/>
<appender-ref ref="stdout"/>
</root>
</configuration>