1. Overview
1.概述
Logback is one of the most popular logging frameworks for Java-based applications. It has built-in support for advanced filtering, archival and removal of old log files, and sending log messages over email.
Logback是基于Java的应用程序中最受欢迎的日志框架之一。它内置支持高级过滤、存档和删除旧的日志文件,以及通过电子邮件发送日志信息。
In this quick tutorial, we’ll configure Logback for sending out an email notification for any application errors.
在这个快速教程中,我们将配置Logback,以便对任何应用程序错误发送电子邮件通知。
2. Setup
2.设置
Logback’s email notification feature requires using a SMTPAppender. The SMTPAppender makes use of the Java Mail API, which in turn depends on the JavaBeans Activation Framework.
Logback的电子邮件通知功能需要使用SMTPAppender。SMTPAppender利用了Java Mail API,而这又依赖于JavaBeans激活框架。
Let’s add those dependencies in our POM:
让我们在我们的POM中添加这些依赖性。
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<scope>runtime</scope>
</dependency>
We can find the latest versions of the Java Mail API and JavaBeans Activation Framework on Maven Central.
我们可以在Maven中心找到Java Mail API和JavaBeans Activation Framework的最新版本。
3. Configuring SMTPAppender
3.配置SMTPAppender
Logback’s SMTPAppender, by default, triggers an email when logging an ERROR event.
Logback的SMTPAppender,默认情况下,在记录ERROR事件时,会触发一封电子邮件。
It holds all the logging events in a cyclic buffer with a default maximum capacity of 256 events. After the buffer gets full, it throws away any older log events.
它将所有的日志事件保存在一个循环缓冲区中,默认最大容量为256个事件。缓冲区满了之后,它会扔掉任何旧的日志事件。
Let’s configure a SMTPAppender in our logback.xml:
让我们在logback.xml中配置一个SMTPAppender。
<appender name="emailAppender" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>OUR-SMTP-HOST-ADDRESS</smtpHost>
<!-- one or more recipients are possible -->
<to>EMAIL-RECIPIENT-1</to>
<to>EMAIL-RECIPIENT-2</to>
<from>SENDER-EMAIL-ADDRESS</from>
<subject>BAELDUNG: %logger{20} - %msg</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</layout>
</appender>
Also, we’ll add this appender to our Logback configuration’s root element:
另外,我们将把这个appender添加到我们Logback配置的root元素中。
<root level="INFO">
<appender-ref ref="emailAppender"/>
</root>
As a result, for any application ERROR that gets logged, it’ll send an email with all the buffered logging events formatted by PatternLayout.
因此,对于任何被记录的应用程序ERROR,它将发送一封电子邮件,其中包含由PatternLayout格式化的所有缓冲的记录事件。
We can further replace the PatternLayout with an HTMLLayout to format the log messages in an HTML table:
我们可以进一步将PatternLayout替换为HTMLLayout,将日志信息格式化为HTML表格:
4. Custom Buffer Size
4.自定义缓冲区大小
We now know that by default, the outgoing email will contain the last 256 logging event messages. However, we can customize this behavior by including the cyclicBufferTracker configuration and specifying the desired bufferSize.
我们现在知道,默认情况下,发出的邮件将包含最后256条日志事件信息。然而,我们可以通过包括cyclicBufferTracker配置和指定所需的bufferSize来定制这一行为。
For triggering an email notification that’ll only include the latest five logging events, we’ll have:
对于触发电子邮件通知,只包括最近的五个记录事件,我们将有。
<appender name="emailAppender" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>OUR-SMTP-HOST-ADDRESS</smtpHost>
<to>EMAIL-RECIPIENT</to>
<from>SENDER-EMAIL-ADDRESS</from>
<subject>BAELDUNG: %logger{20} - %msg</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<bufferSize>5</bufferSize>
</cyclicBufferTracker>
</appender>
5. SMTPAppender for Gmail
5.SMTPAppender for Gmail
If we’re using Gmail as our SMTP provider, we’ll have to authenticate over SSL or STARTTLS.
如果我们使用Gmail作为我们的SMTP提供商,我们将不得不通过SSL或STARTTLS进行认证。
To establish a connection over STARTTLS, the client first issues a STARTTLS command to the server. If the server supports this communication, the connection then switches over to SSL.
要通过STARTTLS建立连接,客户首先向服务器发出STARTTLS命令。如果服务器支持这种通信,连接就会切换到SSL。
Let’s now configure our appender for Gmail using STARTTLS:
现在让我们使用STARTTLS来配置我们的Gmail的appender。
<appender name="emailAppender" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>587</smtpPort>
<STARTTLS>true</STARTTLS>
<asynchronousSending>false</asynchronousSending>
<username>SENDER-EMAIL@gmail.com</username>
<password>GMAIL-ACCT-PASSWORD</password>
<to>EMAIL-RECIPIENT</to>
<from>SENDER-EMAIL@gmail.com</from>
<subject>BAELDUNG: %logger{20} - %msg</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
</appender>
6. Conclusion
6.结语
In this article, we explored how to configure a Logback’s SMTPAppender for sending out emails in case of an application error.
在这篇文章中,我们探讨了如何配置Logback的SMTPAppender,以便在应用程序出现错误时发送电子邮件。
As usual, all code samples are available over on Github.
像往常一样,所有的代码样本都可以在Github上找到。