In-depth understanding of SpringBoot log framework: from entry to advanced application - (4) Logback output log to QQ mailbox

To output logs from Logback to QQ mailbox, you need to perform the following steps:

  1. Obtain the authorization code in the QQ mailbox.
  2. Add Logback dependencies and SMTP protocol implementation libraries, such as Email dependencies, to your SpringBoot project.
  3. Add SMTPAppender in Logback configuration file. And configure SMTPAppender, set the SMTP server host name, port number, login mailbox and password, sender mailbox and receiver mailbox. Finally added to the Logger.
  4. Start the project, and when there are logs to be output, Logback will automatically send the logs to the specified QQ email address.

Obtain QQ mailbox authorization code

Log in to the QQ mailbox , click Settings, enable the POP3/SMTP service, and obtain the QQ mailbox authorization code (save it, you will need it later):

image-20210721183022698

add dependencies

Spring Boot uses SLF4J + Logback as the default logging framework, so we don't need to add dependencies again, just add Email dependencies:

<!--mail依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Write SMTPAppender

To output the log to QQ mailbox, you need to use SMTPAppender and SMTP configuration. The following is an example configuration of the logback-spring.xml file:

<configuration>
  <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <!-- SMTP server的地址 -->
    <smtpHost>${user.host}</smtpHost>
    <!-- SMTP server的端口地址 -->
    <smtpPort>465</smtpPort>
    <!--发件人账号-->
    <username>${user.email}</username>
    <!--发件人授权码-->
    <password>${user.email.password}</password>
    <!--SSL连接到日志服务器,默认值:false-->
    <SSL>true</SSL>
    <!--异步发送-->
    <asynchronousSending>true</asynchronousSending>
    <!--收件人账号,多个用逗号隔开-->
    <to>${user.email}</to>
    <!-- 发件人名称 -->
    <from>${user.email}</from>
    <!-- emial的标题 -->
    <subject>【Error】:%logger{0}</subject>
    <!-- 编码 -->
    <charsetEncoding>UTF-8</charsetEncoding>

    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
      <!-- 每个电子邮件只发送一个日志条目 -->
      <bufferSize>10</bufferSize>
    </cyclicBufferTracker>

    <!--HTML展示-->
    <layout class="ch.qos.logback.classic.html.HTMLLayout" />
    <!--文本展示-->
    <!--<layout class="ch.qos.logback.classic.layout.TTLLLayout"/>-->

    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <!--错误级别(只会提示大于该级别的错误)-->
      <level>ERROR</level>
    </filter>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="EMAIL" />
  </root>
  
</configuration>

In the above configuration, you need to replace the following:

  1. SMTP server hostname and port number;
  2. QQ email address and authorization code, here is your QQ email address and authorization code;
  3. Recipient's address, here is the recipient's email address, you can write multiple, separated by commas;
  4. Sender address, here is your QQ email address;
  5. Email subject, here is the email subject sent;
  6. The layout used, here uses HTML layout, you can also choose other layouts.

operation result

Write the method and output the log:

public static void main(String[] args) {
    
    
  logger.info("日志测试");
  logger.trace("日志测试");
  logger.error("日志测试1");
  logger.error("日志测试2");
  logger.error("日志测试3");
}

After the configuration is complete, Logback can output logs to your QQ mailbox.

image-20230616010036376

Guess you like

Origin blog.csdn.net/qq_20185737/article/details/131238561