SpringBoot整合logback输出不同级别日志到不同文件

通常我们希望日志文件自定义输出位置,并且每天的日志放在一个文件里,有时候可能需要将 info 的日志和 error 的日志分开放,毕竟我们更关注 error 的日志。

可以通过下面一个简单的配置文件实现!

 

一、logback配置

pom.xml

  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-logging</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.codehaus.janino</groupId>
  7.     <artifactId>janino</artifactId>
  8.     <version>3.0.7</version>
  9. </dependency>

 

application.yml

  1. logging:
  2.   config: classpath:logback.xml

 

在reources目录下创建 logback.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="60 seconds" debug="true" >
  3. <!--    <springProperty scope="context" name="contextName" source="spring.application.name"/>-->
  4. <!--    <springProperty scope="context" name="logPath" source="logback.logpath"/>-->
  5.     <property name="contextName" value="Log-Demo" />
  6.     <property name="logPath" value="D:/log2" />
  7.     <contextName>${contextName}</contextName>
  8.     <property name="LOG_HOME" value="${logPath}/${contextName}" />
  9.     <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %X{reqId} %X{serverIp} %X{serverName} %-5level %logger{100} - %msg%n" />
  10.     <!-- info日志 appender  -->
  11.     <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
  12.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  13.             <!-- 按天回滚 daily -->
  14.             <fileNamePattern>${LOG_HOME}/${contextName}-info.%d{yyyy-MM-dd}.log</fileNamePattern>
  15.             <!-- 日志最大的历史 30天 -->
  16.             <maxHistory>30</maxHistory>
  17.         </rollingPolicy>
  18.         <encoder>
  19.             <pattern>${LOG_PATTERN}</pattern>
  20.             <charset class="java.nio.charset.Charset">UTF-8</charset>
  21.         </encoder>
  22.         <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
  23.             <level>INFO</level>
  24.             <onMatch>ACCEPT</onMatch>
  25.             <onMismatch>DENY</onMismatch>
  26.         </filter>
  27.     </appender>
  28.     <appender name="WARN"  class="ch.qos.logback.core.rolling.RollingFileAppender">
  29.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  30.             <FileNamePattern>${LOG_HOME}/${contextName}-warn.%d{yyyy-MM-dd}.log</FileNamePattern>
  31.             <maxHistory>30</maxHistory>
  32.         </rollingPolicy>
  33.         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  34.             <pattern>${LOG_PATTERN}</pattern>
  35.             <charset class="java.nio.charset.Charset">UTF-8</charset>
  36.         </encoder>
  37.         <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
  38.             <level>WARN</level>
  39.             <onMatch>ACCEPT</onMatch>
  40.             <onMismatch>DENY</onMismatch>
  41.         </filter>
  42.     </appender>
  43.     <appender name="ERROR"  class="ch.qos.logback.core.rolling.RollingFileAppender">
  44.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  45.             <FileNamePattern>${LOG_HOME}/${contextName}-error.%d{yyyy-MM-dd}.log</FileNamePattern>
  46.             <maxHistory>30</maxHistory>
  47.         </rollingPolicy>
  48.         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  49.             <pattern>${LOG_PATTERN}</pattern>
  50.             <charset class="java.nio.charset.Charset">UTF-8</charset>
  51.         </encoder>
  52.         <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
  53.             <level>ERROR</level>
  54.             <onMatch>ACCEPT</onMatch>
  55.             <onMismatch>DENY</onMismatch>
  56.         </filter>
  57.     </appender>
  58.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  59.         <encoder>
  60.             <pattern>${LOG_PATTERN}</pattern>
  61.             <charset class="java.nio.charset.Charset">UTF-8</charset>
  62.         </encoder>
  63.     </appender>
  64.     <logger name="com.example.demo" level="DEBUG"/>
  65.     <logger name="org.springframework" level="INFO"/>
  66.     <!--日志打印的包的范围,及分类日志文件存储 -->
  67.     <logger name="com.example.demo" additivity="false">
  68.         <level value="DEBUG" />
  69.         <appender-ref ref="STDOUT"/>
  70.         <appender-ref ref="ERROR"/>
  71.         <appender-ref ref="WARN"/>
  72.         <appender-ref ref="INFO" />
  73.     </logger>
  74.     <root level="INFO" additivity="false">
  75.         <appender-ref ref="STDOUT" />
  76.     </root>
  77. </configuration>

注意修改 contextName 和 logPath的值(6-7行),还有日志打包的范围(77行和73行)

 

二、日志输出

通常有两种方法,一种是引入 lombok 依赖,使用在类上加 @Slfj 注解,然后在方法里直接使用 log.info(""); 这种;还有一种是传统方法,创建 Logger 实例,如下代码

  1. package com.example.demo.controller;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @Slf4j //方法一
  10. @RequestMapping("/test")
  11. public class MainController {
  12. //    private static Logger log = LoggerFactory.getLogger(MainController.class); //方法二
  13.     @GetMapping("/info")
  14.     public String info() {
  15.         log.info("这是一条info级别的日志");
  16.         return "info";
  17.     }
  18.     @GetMapping("/warn")
  19.     public String warning() {
  20.         log.warn("这是一条warning级别的日志");
  21.         return "warning";
  22.     }
  23.     @GetMapping("/error")
  24.     public String error() {
  25.         log.error("这是一条error级别的日志");
  26.         return "error";
  27.     }
  28. }

111

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
言曌

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: