user3919727
user3919727

Reputation: 343

logback smtpAppender mailing on both error and info log level

I am trying to use logback SMTP appender to send email alerts. I am getting emails only when log level is 'error' (I know this is by default). How do I get emails when log level is being set to 'info'?

<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>

In the java class I am invoking this as below

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

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

I would also like to send success emails when log level is being set to 'info'

<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>

In the java class I am invoking this something like below

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

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

I would like to handle both together. Thanks in advance.

Upvotes: 0

Views: 585

Answers (1)

squal
squal

Reputation: 60

include below after layout in your email appender. It should filter out info logs.

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

Upvotes: 0

Related Questions