springboot pay the log configuration items

Logging framework

This section content:

1: several common logging framework

2: Logback use

3: how the info and error logs to a different level by date file and a file every day.

document_image_rId9.png

document_image_rId10.png


More than a few frames may be classified as follows:

document_image_rId11.png

document_image_rId12.png

Use of SLF4J and Logback

Log Level:

document_image_rId13.png

Bigger and more serious level.

A: Java classes using the log in two ways:

One way: Add Log objects in each class:

private  final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

document_image_rId14.png

Method Two: Use lombok plugin:

Add in the pom.xml file lombok dependent:

<dependency>

  <groupId>org.projectlombok</groupId>

  <artifactId>lombok</artifactId>

  <optional>true</optional>

</dependency>

Then use the above classes annotated @ slf4j

document_image_rId15.png

推荐使用第二种,注解方式。这样就不用再每个类中添加了。

二:变量的打印

方式一:使用字符串+的方式。

document_image_rId16.png

直接使用字符串+的。如果变量多,+就需要很多了。

方式二:使用占位符的方式。

document_image_rId17.png

两个运行的结果:

document_image_rId18.png

推荐使用第二种,这样一看及明白。

Logback的配置

两种配置方式:

application.yml和logback-spring.xml

也就是一种基于yml文件一种基于xml文件配置的

来看下项目中经常遇到的日志需求:

区分info和error日志;每天产生一个日志文件。

document_image_rId19.png

方式一:再yml文件中配置

document_image_rId20.png

我们可以看到,logging相关配置还很多的。如console打印的日志格式、日期格式、文件、日志级别、日志最大大小等等。都可以配置的。

如:我们配置在控制台输出时间-信息换行。这个怎么配置呢?

document_image_rId21.png

查看运行结果:

document_image_rId22.png

配置日志输出位置:

document_image_rId23.png

运行后,可以在C盘下看到一个aa文件夹,打开就是日志文件了。如下图:

document_image_rId24.png

默认日志文件名称是spring.log。如果想修改成自己的可以使用file。如下图

document_image_rId25.png

运行后:

document_image_rId26.png

修改日志级别:

document_image_rId27.png

日志级别还可以绑定到指定的类上面。如:

document_image_rId28.png

第二种方式:使用xml文件配置

1:控制台输出:

document_image_rId29.png

2:根据不同日志级别输出到不同日志文件中。文件名带上日期

可以看到,文件滚动方式有很多,可以安装大小和时间、可以按照时间的。如下图:

document_image_rId30.png

配置后文件如下:

document_image_rId31.png

基于XML配置的所有配置信息:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

   <!-- 控制台输入日志格式配置-->

   <appender name="consolelogg" class="ch.qos.logback.core.ConsoleAppender">

       <layout class="ch.qos.logback.classic.PatternLayout">

           <pattern>

               %d -%msg%n

           </pattern>

       </layout>

   </appender>


   <!-- info日志 每天一个日志文件配置-->

   <appender name="infoFileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">

       <filter class="ch.qos.logback.classic.filter.LevelFilter">

           <level>ERROR</level>

           <onMatch>DENY</onMatch>

           <onMismatch>ACCEPT</onMismatch>

       </filter>

       <encoder>

           <pattern>

               %msg%n

           </pattern>

       </encoder>

       <!-- 配置日志滚动策略-->

       <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

           <!-- 文件路径.文件名称中带有日期-->

           <fileNamePattern>c:/aa/info.%d.log</fileNamePattern>

       </rollingPolicy>

   </appender>



   <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">

       <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

           <level>ERROR</level>

       </filter>

       <encoder>

           <pattern>

               %msg%n

           </pattern>

       </encoder>

       <!--滚动策略-->

       <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

           <!--路径-->

           <fileNamePattern>c:/aa/error.%d.log</fileNamePattern>

       </rollingPolicy>

   </appender>



   <root level="info">

       <appender-ref ref="consolelogg"/>

       <appender-ref ref="infoFileLog"/>

       <appender-ref ref="fileErrorLog"/>

   </root>

</configuration>

What we have introduced to better logging framework chant


Guess you like

Origin blog.51cto.com/kaigejava/2439235