0

我正在尝试使用 logback SMTP appender 发送电子邮件警报。只有当日志级别为“错误”时,我才会收到电子邮件(我知道这是默认设置)。将日志级别设置为“信息”时如何接收电子邮件?

<property resource="application.properties"/>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
      <smtpHost>${spring.mail.host}</smtpHost>
     <username>${spring.mail.username}</username>
     <password>${spring.mail.password}</password>
     <smtpPort>${spring.mail.port}</smtpPort>
     <STARTTLS>false</STARTTLS>
     <SSL>true</SSL>
     <subject>Exception: Registraion App %m</subject>
     <to>${spring.mail.to}</to>
     <from>${spring.mail.from}</from>
     <asynchronousSending>true</asynchronousSending>
    <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%date %-5level %logger{35} - %message%n</pattern>
    </layout>
</appender>

<logger name="errorLogger" level="error" additivity="false">
   <appender-ref ref="EMAIL"/>
</logger>

在java类中我调用它如下

public static final Logger emailExceptionLOGGER = LoggerFactory.getLogger("errorLogger");

   try{
         .....
      }catch(Exception e){
         emailExceptionLOGGER.error("To send exception email")   
     }

当日志级别设置为“信息”时,我还想发送成功电子邮件

<appender name="SUCCESS-EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
      <smtpHost>${spring.mail.host}</smtpHost>
      <username>${spring.mail.username}</username>
      <password>${spring.mail.password}</password>
      <smtpPort>${spring.mail.port}</smtpPort>
      <STARTTLS>false</STARTTLS>
      <SSL>true</SSL>
      <subject>Registraion App %m</subject>
      <to>${spring.mail.to}</to>
      <from>${spring.mail.from}</from>
      <asynchronousSending>true</asynchronousSending>
      <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%date %-5level %logger{35} - %message%n</pattern>
     </layout>
</appender>

<logger name="successLogger" level="info" additivity="false">
    <appender-ref ref="SUCCESS-EMAIL"/>
</logger>

在 java 类中,我正在调用类似下面的内容

public static final Logger emailSuccessLOGGER = LoggerFactory.getLogger("successLogger");

  if(success){
      emailSuccessLOGGER.info("To send success email")   
  }

我想同时处理这两个问题。提前致谢。

4

1 回答 1

0

在您的电子邮件附加程序中的布局后包括以下内容。它应该过滤掉信息日志。

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>ERROR</level>
</filter>
于 2019-07-08T05:24:26.467 回答