热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot2.1版本使用logback日志(自己已经验证)

https:blog.csdn.netweixin_40593587articledetails86622009版权声明:本文为博主原创文章,遵循CC4

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文件如下:

  1. xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0modelVersion>
  5. <parent>
  6. <groupId>org.springframework.bootgroupId>
  7. <artifactId>spring-boot-starter-parentartifactId>
  8. <version>2.1.2.RELEASEversion>
  9. <relativePath/>
  10. parent>
  11. <groupId>com.example.phonegroupId>
  12. <artifactId>demoartifactId>
  13. <version>0.0.1-SNAPSHOTversion>
  14. <name>demoname>
  15. <description>Demo project for Spring Bootdescription>
  16. <properties>
  17. <java.version>1.8java.version>
  18. properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.bootgroupId>
  22. <artifactId>spring-boot-starter-webartifactId>
  23. dependency>
  24. <dependency>
  25. <groupId>org.springframework.bootgroupId>
  26. <artifactId>spring-boot-starter-freemarkerartifactId>
  27. dependency>
  28. <dependency>
  29. <groupId>org.mybatis.spring.bootgroupId>
  30. <artifactId>mybatis-spring-boot-starterartifactId>
  31. <version>1.3.2version>
  32. dependency>
  33. <dependency>
  34. <groupId>com.alibabagroupId>
  35. <artifactId>fastjsonartifactId>
  36. <version>1.2.46version>
  37. dependency>
  38. <dependency>
  39. <groupId>mysqlgroupId>
  40. <artifactId>mysql-connector-javaartifactId>
  41. <scope>runtimescope>
  42. dependency>
  43. <dependency>
  44. <groupId>org.springframework.bootgroupId>
  45. <artifactId>spring-boot-starter-testartifactId>
  46. <scope>testscope>
  47. dependency>
  48. dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.bootgroupId>
  53. <artifactId>spring-boot-maven-pluginartifactId>
  54. plugin>
  55. plugins>
  56. build>
  57. project>

接下来我们需要在resources目录下建立一个xml文件&#xff0c;就叫做logback-spring.xml;为什么要起这个名字呢&#xff0c;因为起这个名字springboot2.1会自己去找该文件&#xff0c;里面内容如下&#xff1a;

  1. xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
  2. <configuration>
  3. <appender name&#61;"consoleApp" class&#61;"ch.qos.logback.core.ConsoleAppender">
  4. <layout class&#61;"ch.qos.logback.classic.PatternLayout">
  5. <pattern>
  6. %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
  7. pattern>
  8. layout>
  9. appender>
  10. <appender name&#61;"fileInfoApp" class&#61;"ch.qos.logback.core.rolling.RollingFileAppender">
  11. <filter class&#61;"ch.qos.logback.classic.filter.LevelFilter">
  12. <level>ERRORlevel>
  13. <onMatch>DENYonMatch>
  14. <onMismatch>ACCEPTonMismatch>
  15. filter>
  16. <encoder>
  17. <pattern>
  18. %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
  19. pattern>
  20. encoder>
  21. <rollingPolicy class&#61;"ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  22. <fileNamePattern>app_log/log/app.info.%d.logfileNamePattern>
  23. rollingPolicy>
  24. appender>
  25. <appender name&#61;"fileErrorApp" class&#61;"ch.qos.logback.core.rolling.RollingFileAppender">
  26. <filter class&#61;"ch.qos.logback.classic.filter.ThresholdFilter">
  27. <level>ERRORlevel>
  28. filter>
  29. <encoder>
  30. <pattern>
  31. %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
  32. pattern>
  33. encoder>
  34. <rollingPolicy class&#61;"ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  35. <fileNamePattern>app_log/log/app.err.%d.logfileNamePattern>
  36. <MaxHistory>1MaxHistory>
  37. rollingPolicy>
  38. appender>
  39. <root level&#61;"INFO">
  40. <appender-ref ref&#61;"consoleApp"/>
  41. <appender-ref ref&#61;"fileInfoApp"/>
  42. <appender-ref ref&#61;"fileErrorApp"/>
  43. root>
  44. configuration>

这样子就可以了&#xff0c;这样我们在控制器就可以使用了&#xff1a;

import org.slf4j.LoggerFactory;、//注意导包的时候是导进去这个包&#xff1b;

private org.slf4j.Logger logger&#61;LoggerFactory.getLogger(this.getClass());

调用接口如下&#xff1a;

  1. &#64;RequestMapping("/addInformation")
  2. public Map addUser(&#64;RequestBody Information information) {
  3. System.out.println("开始新增...");
  4. System.out.println(information);
  5. System.out.println(information.getImei());
  6. System.out.println(information.getKey());
  7. boolean flag&#61; informationService.addInformation(information);
  8. // return flag;
  9. logger.info(" i am info");
  10. logger.error("i am error");
  11. logger.warn(" i am warn");
  12. logger.debug(" i am debug");
  13. 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;

 

 

 

运行访问成功。

 


推荐阅读
author-avatar
Cindere丷lla_茹满
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有