错误和信息日志级别的logback smtpAppender邮件

时间:2019-01-08 23:28:30

标签: logback smtpappender

我正在尝试使用Logback SMTP附加程序发送电子邮件警报。 我仅在日志级别为“错误”时才收到电子邮件(我知道默认情况下)。 将日志级别设置为“信息”时,如何获取电子邮件?

<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")   
  }

我想一起处理。预先感谢。

1 个答案:

答案 0 :(得分:0)

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

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