Spring Boot日志框架实践

ekooz01 1年前
   <p><img src="https://simg.open-open.com/show/5fdc485042b478dcbedcc478e70b30eb.jpg"></p>    <h2>概述</h2>    <p>Java应用中,日志一般分为以下5个级别:</p>    <ul>     <li>ERROR 错误信息</li>     <li>WARN 警告信息</li>     <li>INFO 一般信息</li>     <li>DEBUG 调试信息</li>     <li>TRACE 跟踪信息</li>    </ul>    <p>Spring Boot使用Apache的Commons Logging作为内部的日志框架,其仅仅是一个日志接口,在实际应用中需要为该接口来指定相应的日志实现。</p>    <p>SpringBt默认的日志实现是Java Util Logging,是JDK自带的日志包,此外SpringBt当然也支持Log4J、Logback这类很流行的日志实现。</p>    <p>统一将上面这些 <strong>日志实现</strong> 统称为 <strong>日志框架</strong></p>    <p>下面我们来实践一下!</p>    <h2>使用Spring Boot Logging插件</h2>    <ul>     <li>首先application.properties文件中加配置:</li>    </ul>    <pre>  <code class="language-java">logging.level.root=INFO</code></pre>    <ul>     <li>控制器部分代码如下:</li>    </ul>    <pre>  <code class="language-java">package com.hansonwang99.controller;    import com.hansonwang99.K8sresctrlApplication;  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;    @RestController  @RequestMapping("/testlogging")  public class LoggingTestController {      private static Logger logger = LoggerFactory.getLogger(K8sresctrlApplication.class);      @GetMapping("/hello")      public String hello() {          logger.info("test logging...");          return "hello";      }  }</code></pre>    <ul>     <li>运行结果</li>    </ul>    <p><img src="https://simg.open-open.com/show/56ae200da1bdb3b84800a6bd46b288f5.png"></p>    <p>由于将日志等级设置为INFO,因此包含INFO及以上级别的日志信息都会打印出来</p>    <p>这里可以看出,很多大部分的INFO日志均来自于SpringBt框架本身,如果我们想屏蔽它们,可以将日志级别统一先全部设置为ERROR,这样框架自身的INFO信息不会被打印。然后再将应用中特定的包设置为DEBUG级别的日志,这样就可以只看到所关心的包中的DEBUG及以上级别的日志了。</p>    <ul>     <li>控制特定包的日志级别</li>    </ul>    <p>application.yml中改配置</p>    <pre>  <code class="language-java">logging:    level:      root: error      com.hansonwang99.controller: debug</code></pre>    <p>很明显,将root日志级别设置为ERROR,然后再将 com.hansonwang99.controller 包的日志级别设为DEBUG,此即:即先禁止所有再允许个别的 设置方法</p>    <ul>     <li>控制器代码</li>    </ul>    <pre>  <code class="language-java">package com.hansonwang99.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;    @RestController  @RequestMapping("/testlogging")  public class LoggingTestController {      private Logger logger = LoggerFactory.getLogger(this.getClass());      @GetMapping("/hello")      public String hello() {          logger.info("test logging...");          return "hello";      }  }</code></pre>    <ul>     <li>运行结果</li>    </ul>    <p><img src="https://simg.open-open.com/show/7055b4fc6f4c343896e80356294cdf68.png"></p>    <p>可见框架自身的INFO级别日志全部藏匿,而指定包中的日志按级别顺利地打印出来</p>    <ul>     <li>将日志输出到某个文件中</li>    </ul>    <pre>  <code class="language-java">logging:    level:      root: error      com.hansonwang99.controller: debug    file: ${user.home}/logs/hello.log</code></pre>    <ul>     <li>运行结果</li>    </ul>    <p><img src="https://simg.open-open.com/show/b932e65ff50968693783a9b6e7b604e5.png"></p>    <p><img src="https://simg.open-open.com/show/cb2fb5dee14a5adecc095db5ac8f2282.png"></p>    <p>使用Spring Boot Logging,我们发现虽然日志已输出到文件中,但控制台中依然会打印一份,发现用 org.slf4j.Logger 是无法解决这个问题的</p>    <p><img src="https://simg.open-open.com/show/d2d0a8aeb885f02c602884b9c4b21e81.png"></p>    <h2>集成Log4J日志框架</h2>    <ul>     <li>pom.xml中添加依赖</li>    </ul>    <pre>  <code class="language-java"><dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>              <exclusions>                  <exclusion>                      <groupId>org.springframework.boot</groupId>                      <artifactId>spring-boot-starter-logging</artifactId>                  </exclusion>              </exclusions>          </dependency>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-log4j2</artifactId>          </dependency></code></pre>    <ul>     <li>在resources目录下添加 log4j2.xml 文件,内容如下:</li>    </ul>    <pre>  <code class="language-java"><?xml version="1.0" encoding="UTF-8"?>  <configuration>        <appenders>          <File name="file" fileName="${sys:user.home}/logs/hello2.log">              <PatternLayout pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"/>          </File>      </appenders>        <loggers>            <root level="ERROR">              <appender-ref ref="file"/>          </root>          <logger name="com.hansonwang99.controller" level="DEBUG" />      </loggers>    </configuration></code></pre>    <ul>     <li>其他代码都保持不变</li>    </ul>    <p>运行程序发现控制台没有日志输出,而hello2.log文件中有内容,这符合我们的预期:</p>    <p><img src="https://simg.open-open.com/show/21757b60e75efc66f5c8ced456c6f5f0.png"></p>    <p><img src="https://simg.open-open.com/show/85c28508b6bdfa6b61dfa7a66e0fbc30.png"></p>    <p><img src="https://simg.open-open.com/show/a0ba8a78ecf255afe1c0097847b16392.png"></p>    <p>而且日志格式和 pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n" 格式中定义的相匹配</p>    <h2>Log4J更进一步实践</h2>    <ul>     <li>pom.xml配置:</li>    </ul>    <pre>  <code class="language-java"><dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>              <exclusions>                  <exclusion>                      <groupId>org.springframework.boot</groupId>                      <artifactId>spring-boot-starter-logging</artifactId>                  </exclusion>              </exclusions>          </dependency>            <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-log4j2</artifactId>          </dependency></code></pre>    <ul>     <li>log4j2.xml配置</li>    </ul>    <pre>  <code class="language-java"><?xml version="1.0" encoding="UTF-8"?>  <configuration status="warn">      <properties>            <Property name="app_name">springboot-web</Property>          <Property name="log_path">logs/${app_name}</Property>        </properties>      <appenders>          <console name="Console" target="SYSTEM_OUT">              <PatternLayout pattern="[%d][%t][%p][%l] %m%n" />          </console>            <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"                       filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">              <Filters>                  <ThresholdFilter level="INFO" />                  <ThresholdFilter level="WARN" onMatch="DENY"                                   onMismatch="NEUTRAL" />              </Filters>              <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />              <Policies>                  <!-- 归档每天的文件 -->                  <TimeBasedTriggeringPolicy interval="1" modulate="true" />                  <!-- 限制单个文件大小 -->                  <SizeBasedTriggeringPolicy size="2 MB" />              </Policies>              <!-- 限制每天文件个数 -->              <DefaultRolloverStrategy compressionLevel="0" max="10"/>          </RollingFile>            <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"                       filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">              <Filters>                  <ThresholdFilter level="WARN" />                  <ThresholdFilter level="ERROR" onMatch="DENY"                                   onMismatch="NEUTRAL" />              </Filters>              <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />              <Policies>                  <!-- 归档每天的文件 -->                  <TimeBasedTriggeringPolicy interval="1" modulate="true" />                  <!-- 限制单个文件大小 -->                  <SizeBasedTriggeringPolicy size="2 MB" />              </Policies>              <!-- 限制每天文件个数 -->              <DefaultRolloverStrategy compressionLevel="0" max="10"/>          </RollingFile>            <RollingFile name="RollingFileError" fileName="${log_path}/error.log"                       filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">              <ThresholdFilter level="ERROR" />              <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />              <Policies>                  <!-- 归档每天的文件 -->                  <TimeBasedTriggeringPolicy interval="1" modulate="true" />                  <!-- 限制单个文件大小 -->                  <SizeBasedTriggeringPolicy size="2 MB" />              </Policies>              <!-- 限制每天文件个数 -->              <DefaultRolloverStrategy compressionLevel="0" max="10"/>          </RollingFile>        </appenders>        <loggers>              <root level="info">              <appender-ref ref="Console" />              <appender-ref ref="RollingFileInfo" />              <appender-ref ref="RollingFileWarn" />              <appender-ref ref="RollingFileError" />          </root>        </loggers>    </configuration></code></pre>    <ul>     <li>控制器代码:</li>    </ul>    <pre>  <code class="language-java">package com.hansonwang99.controller;    import org.apache.logging.log4j.LogManager;  import org.apache.logging.log4j.Logger;  import org.springframework.web.bind.annotation.GetMapping;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RestController;    @RestController  @RequestMapping("/testlogging")  public class LoggingTestController {      private final Logger logger = LogManager.getLogger(this.getClass());      @GetMapping("/hello")      public String hello() {          for(int i=0;i<10_0000;i++){              logger.info("info execute index method");              logger.warn("warn execute index method");              logger.error("error execute index method");          }          return "My First SpringBoot Application";      }  }</code></pre>    <ul>     <li>运行结果</li>    </ul>    <p><img src="https://simg.open-open.com/show/a420d8346b86560386a570384370ecd3.png"></p>    <p><img src="https://simg.open-open.com/show/b7a9196cc68497c20397e1d3ec0dc750.png"></p>    <p><img src="https://simg.open-open.com/show/3dbe75b9637038ca9c807be29308ef84.png"></p>    <p>日志会根据不同的级别存储在不同的文件,当日志文件大小超过2M以后会分多个文件压缩存储,生产环境的日志文件大小建议调整为20-50MB。</p>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000014055496</p>    <p> </p>