Spring boot integrated configuration logback-spring.xml

Keywords: Programming Spring SSL xml log4j

Using logback instead of log4j is because logback is another open source log component designed by the founder of log4j. The kernel of logback has been rewritten and its performance has been improved by more than 10 times on some key execution paths. Moreover, the logback not only improves the performance, but also reduces the initial memory load. Moreover, the official website has very sufficient documents. Through configuration, the log files can be removed and so on. It has many advantages and is very powerful. It is also a popular spring boot match.

Create logback-spring.xml file

First, create a logback in the resources directory- spring.xml File, why is that name? The reason is that logback will read this file from the resource directory by default, and using this name has the advantage of using spring property and spring profile tags.

Secondly, the spring boot starter parent dependency already includes logback classic, log4j to slf4j dependency, so we do not need to pom.xml Add dependency again in the file. What do you think? ctrl + click spring boot starter parent -- > spring boot dependencies -- > spring boot starter -- > spring boot starter logging -- >

Configure logback-spring.xml content

<!--?xml version="1.0" encoding="UTF-8"?-->
<configuration scan="true" scanperiod="60 seconds" debug="false">
   <contextname>logback</contextname>
   <property resource="application.yml" />

   <springproperty scope="context" name="log.path" source="logging.path" />
   <springproperty scope="context" name="log.lv" source="logging.lv" />
   <springproperty scope="context" name="log.dateSize" source="logging.dateSize" />

   <!--Output to console-->
   <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
       <!--This log appender It is used for development. Only the lowest level is configured. The log level output by the console is the log information greater than or equal to this level-->
       <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
           <level>${log.lv}</level>
       </filter>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
           <!--code-->
           <charset>utf-8</charset>
       </encoder>
   </appender>

   <!--Output to debug-->
   <appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
       <!-- Path and filename of the log file being logged -->
       <file>${log.path}/logback-debug.log</file>
       <!-- Rolling strategy for loggers, by date, by size -->
       <rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <filenamepattern>${log.path}/logback-debug-%d{yyyy-MM-dd}_%i.log</filenamepattern>
           <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
               <maxfilesize>100MB</maxfilesize>
           </timebasedfilenamingandtriggeringpolicy>
           <!--Log file retention days-->
           <maxhistory>15</maxhistory>
       </rollingpolicy>
       <append>true</append>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
           <!--code-->
           <charset>utf-8</charset>
       </encoder>
       <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- Print only DEBUG journal -->
           <level>DEBUG</level>
           <onmatch>ACCEPT</onmatch>
           <onmismatch>DENY</onmismatch>
       </filter>
   </appender>

   <!--Output to info-->
   <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
       <!-- Path and filename of the log file being logged -->
       <file>${log.path}/logback-info.log</file>
       <!-- Rolling strategy for loggers, by date, by size -->
       <rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <filenamepattern>${log.path}/logback-info-%d{yyyy-MM-dd}_%i.log</filenamepattern>
           <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
               <maxfilesize>100MB</maxfilesize>
           </timebasedfilenamingandtriggeringpolicy>
           <!--Log file retention days-->
           <maxhistory>${log.dateSize}</maxhistory>
       </rollingpolicy>
       <append>true</append>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
           <!--code-->
           <charset>utf-8</charset>
       </encoder>
       <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- Print only INFO journal -->
           <level>INFO</level>
           <onmatch>ACCEPT</onmatch>
           <onmismatch>DENY</onmismatch>
       </filter>
   </appender>

   <!--Output to warn-->
   <appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
       <!-- Path and filename of the log file being logged -->
       <file>${log.path}/logback-warn.log</file>
       <!-- Rolling strategy for loggers, by date, by size -->
       <rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <filenamepattern>${log.path}/logback-warn-%d{yyyy-MM-dd}_%i.log</filenamepattern>
           <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
               <maxfilesize>100MB</maxfilesize>
           </timebasedfilenamingandtriggeringpolicy>
           <!--Log file retention days-->
           <maxhistory>${log.dateSize}</maxhistory>
       </rollingpolicy>
       <append>true</append>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
           <charset>utf-8</charset>
       </encoder>
       <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- Print only WARN journal -->
           <level>WARN</level>
           <onmatch>ACCEPT</onmatch>
           <onmismatch>DENY</onmismatch>
       </filter>
   </appender>

   <!--Output to error-->
   <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
       <!-- Path and filename of the log file being logged -->
       <file>${log.path}/logback-error.log</file>
       <!-- Rolling strategy for loggers, by date, by size -->
       <rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <filenamepattern>${log.path}/logback-error-%d{yyyy-MM-dd}_%i.log</filenamepattern>
           <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
               <maxfilesize>100MB</maxfilesize>
           </timebasedfilenamingandtriggeringpolicy>
           <!--Log file retention days-->
           <maxhistory>${log.dateSize}</maxhistory>
       </rollingpolicy>
       <append>true</append>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
           <charset>utf-8</charset>
       </encoder>
       <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- Print only ERROR journal -->
           <level>ERROR</level>
           <onmatch>ACCEPT</onmatch>
           <onmismatch>DENY</onmismatch>
       </filter>
   </appender>

   <!--Mail configuration-->
   <springproperty scope="context" name="smtpHost" source="email.smtpHost" />
   <!-- SMTP server The port address of. -->
   <springproperty scope="context" name="smtpPort" source="email.smtpPort" />
   <!-- Email account -->
   <springproperty scope="context" name="username" source="email.username" />
   <!-- Send mail password (this password is the client authorization password, not the mailbox login password) -->
   <springproperty scope="context" name="password" source="email.password" />
   <!-- If set to true´╝îappender Will use SSL Connect to the log server. Default: false -->
   <springproperty scope="context" name="SSL" source="email.SSL" />
   <!-- Specify the mailbox of the recipient. Multiple mailboxes can be set. The account of the recipient should be separated by commas -->
   <springproperty scope="context" name="email_to" source="email.email_to" />
   <!-- Specifies the sender name. -->
   <springproperty scope="context" name="email_from" source="email.email_from" />
   <!-- title  -->
   <property name="email_subject" value="[System Error]: %msg" />

   <!-- Sent by mail appender -->
   <appender name="Email" class="ch.qos.logback.classic.net.SMTPAppender">
       <smtphost>${smtpHost}</smtphost>
       <smtpport>${smtpPort}</smtpport>
       <username>${username}</username>
       <password>${password}</password>
       <asynchronoussending>false</asynchronoussending>
       <ssl>${SSL}</ssl>
       <to>${email_to}</to>
       <from>${email_from}</from>
       <subject>${email_subject}</subject>
       <!-- html format -->
       <layout class="ch.qos.logback.classic.html.HTMLLayout">
           <pattern>%date - %level [%thread] %logger{50} %line %message</pattern>
       </layout>
       <!-- Grade filter, specifying ERROR Level send -->
       <filter class="ch.qos.logback.classic.filter.LevelFilter">
           <level>ERROR</level>
           <onmatch>ACCEPT</onmatch>
           <onmismatch>DENY</onmismatch>
       </filter>
       <!-- Send only one log entry per email -->
       <cyclicbuffertracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
           <buffersize>1</buffersize>
       </cyclicbuffertracker>
   </appender>

   <!--Set corresponding log output nodes in different environments -->
   <!--development-->
   <springprofile name="dev">
       <root level="debug">
           <appender-ref ref="console" />
           <appender-ref ref="info" />
           <appender-ref ref="warn" />
           <appender-ref ref="error" />
       </root>
   </springprofile>

   <!--test-->
   <springprofile name="test">
       <root level="warn">
           <appender-ref ref="console" />
           <appender-ref ref="warn" />
           <appender-ref ref="error" />
           <appender-ref ref="Email" />
       </root>
   </springprofile>

   <!--production environment -->
   <springprofile name="pro">
       <root level="info">
           <appender-ref ref="console" />
           <appender-ref ref="debug" />
           <appender-ref ref="info" />
           <appender-ref ref="warn" />
           <appender-ref ref="error" />
           <appender-ref ref="Email" />
       </root>
   </springprofile>

</configuration>

Configure the yml or properties file

spring:
 profiles:
   active: dev #Used to configure the environment

#Log configuration
logging:
 path: C:/WEB-INF/logs #Save log
 lv: info # Console log output level
 dateSize: 1 # Log save days

#Mailbox configuration
email:

  #host

 smtpHost: smtp.qq.com

 #Port (465 or 587)

 smtpPort: 465
 username: *********@qq.com
 password: *******************
 SSL: true
 email_from: *********@qq.com
 email_to: *********@qq.com,*********@qq.com

remarks

Knowledge point 1

Due to logback-spring.xml Prior to application.yml Loaded by the system, so you want to reference application.yml The data in the file needs to be added with < property resource=“ application.yml "/ > how to get the label after introduction? For example, to get the number of days to save logs, you need to introduce < springProperty scope = "context" name=“ log.dateSize " source=" logging.dateSize "/ > the following references only need${ log.dateSize }OK, the value in brackets is the name value of springProperty.

Knowledge point 2

Spring profile corresponds to application.yml In the file spring.profiles.active Value of.

Knowledge point 3

The email password in the configuration file is not the mailbox login password, but the client authorization password. How to obtain and query the QQ mailbox login third-party client to obtain the authorization code is OK.

Posted by 7724 on Thu, 28 May 2020 01:53:36 -0700