https://blog.csdn.net/weixin_40593587/article/details/86622009
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_40593587/article/details/86622009
昨天找了很多的logback的博客发现写的都是很旧的版本根本就用不了,今天自己测试了出来了;下面开始代码:
pom文件如下:
xml version="1.0" encoding="UTF-8"?>
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.2.RELEASEversion>
<groupId>com.example.phonegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<description>Demo project for Spring Bootdescription>
<java.version>1.8java.version>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<artifactId>mysql-connector-javaartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
接下来我们需要在resources目录下建立一个xml文件&#xff0c;就叫做logback-spring.xml;为什么要起这个名字呢&#xff0c;因为起这个名字springboot2.1会自己去找该文件&#xff0c;里面内容如下&#xff1a;
xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
<appender name&#61;"consoleApp" class&#61;"ch.qos.logback.core.ConsoleAppender">
<layout class&#61;"ch.qos.logback.classic.PatternLayout">
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
<appender name&#61;"fileInfoApp" class&#61;"ch.qos.logback.core.rolling.RollingFileAppender">
<filter class&#61;"ch.qos.logback.classic.filter.LevelFilter">
<onMismatch>ACCEPTonMismatch>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
<rollingPolicy class&#61;"ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>app_log/log/app.info.%d.logfileNamePattern>
<appender name&#61;"fileErrorApp" class&#61;"ch.qos.logback.core.rolling.RollingFileAppender">
<filter class&#61;"ch.qos.logback.classic.filter.ThresholdFilter">
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
<rollingPolicy class&#61;"ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>app_log/log/app.err.%d.logfileNamePattern>
<appender-ref ref&#61;"consoleApp"/>
<appender-ref ref&#61;"fileInfoApp"/>
<appender-ref ref&#61;"fileErrorApp"/>
这样子就可以了&#xff0c;这样我们在控制器就可以使用了&#xff1a;
import org.slf4j.LoggerFactory;、
private org.slf4j.Logger logger&#61;LoggerFactory.getLogger(this.getClass());
调用接口如下&#xff1a;
&#64;RequestMapping("/addInformation")
public Map addUser(&#64;RequestBody Information information) {
System.out.println("开始新增...");
System.out.println(information);
System.out.println(information.getImei());
System.out.println(information.getKey());
boolean flag&#61; informationService.addInformation(information);
logger.info(" i am info");
logger.error("i am error");
logger.warn(" i am warn");
logger.debug(" i am debug");
return jsonCommon.SucessRequest();
接下来访问的时候就会显示
并会将info和error的信息打印到了我们指定的文件中&#xff0c;文件是自动生成的&#xff1a;我们在logback-spring.xml中配置了error的信息放在了
app_log/log/app.err.%d.log这个文件&#xff0c;info的信息会放在这个文件
app_log/log/app.info.%d.log
访问接口的时候就会自动生成这些目录和文件&#xff1b;目录如下所示&#xff1a;
运行访问成功。