drenda
drenda

Reputation: 6244

Log4j2 smtp appender doesn't work for root logger

I'm using log4j2.2 in my app. I'd like to receive email when there are some error in the application.

So I configured a smtp appender:

<SMTP name="Mailer" subject="Ecall logs" to="${receipients}"
        from="${from}" smtpHost="${smtpHost}" smtpPort="${smtpPort}"
        smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
        smtpPassword="${smtpPassword}" smtpDebug="true" bufferSize="200"
        ignoreExceptions="false">
</SMTP>

and I added it to the root logger:

<root level="error">
    <appender-ref ref="Console" />
    <appender-ref ref="AsyncFile" />
    <appender-ref ref="Mailer" />
</root>     

unfortunately with this configuration I never receive email when there are errors in the app. In fact I've also some others custom loggers:

<logger level="info" name="org.flywaydb" additivity="false">
    <appender-ref ref="Console" />
    <appender-ref ref="AsyncFile" />
</logger>

Which is the right way to configure an smtp logger for the whole application (catch all errors from all packages) with 100 lines of buffers in order to see what happened before/after the error?

Upvotes: 0

Views: 1282

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

You may have found a bug. The SMTP appender should send an email when it receives an ERROR-level log event. It will try to include "bufferSize" events (from TRACE to WARN) that preceded the error.

I don't think that the SMPT appender requires any preceding non-ERROR events, but can you try this:

<root level="trace">
  <appender-ref ref="Console" level="error"/>
  <appender-ref ref="AsyncFile" level="error"/>
  <appender-ref ref="Mailer" level="trace"/>
</root> 

If the problem persists please raise a ticket on the log4j2 Jira issue tracker.

Upvotes: 2

Related Questions