log4j2 SMTP Appender: How to include previous messages with another level?
Asked Answered
G

1

6

I'm using log4j2-beta9 and I have the following config (part of it):

<Appenders>
    <SMTP name="Mailer" suppressExceptions="false"
          subject="${subject}" to="${receipients}" from="${from}"
          smtpHost="${smtpHost}" smtpPort="${smtpPort}"
          smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
          smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="20">
        <PatternLayout>
            <pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %m%n</pattern>
        </PatternLayout>
    </SMTP>

    <Async name="AsyncMailer">
        <AppenderRef ref="Mailer"/>
    </Async>
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="AsyncMailer" level="error"/>
    </Root>
</Loggers>

With this configuration I receive email with just 1 (one) error message. How can I configure log4j2 to receive 1 error message and N previous messages with LEVEL=INFO?

Thanks in advance.

Galantine answered 14/1, 2014 at 13:55 Comment(0)
C
8

This is currently not possible: the SMTP appender has a buffer where it captures log events before emailing them, but it will only capture the events that are configured for the target level (ERROR in your example). If you changed this to INFO, you would get email notifications for all INFO-level log messages (not just the ones preceding an ERROR-level message).

You can raise this as a feature request on the log4j issue tracker or log4j-user mailing list.


Correction: I was wrong, wrong, WRONG!

STMP Appender does have a buffer but it is meant to capture the last X (512 by default) INFO, DEBUG, TRACE-level messages that preceded the ERROR log event. So it is supposed to work like you expected (like the log4j-1.x SMTP appender works).

The SMTP Appender will fire an email when it gets an ERROR (or more severe)-level log event. So in your config you should not only send ERROR-level log events to this appender (or you'll miss the INFO, DEBUG, TRACE events that precede it).

In your config, change <AppenderRef ref="AsyncMailer" level="error"/> to <AppenderRef ref="AsyncMailer"/>.

That should fix the issue. If you still experience problems, someone else reported a similar issue and apparently found a workaround by adding a ThresholdFilter to the configuration:

<Appenders>
    <SMTP name="Mailer" suppressExceptions="false"
          subject="${subject}" to="${receipients}" from="${from}"
          smtpHost="${smtpHost}" smtpPort="${smtpPort}"
          smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
          smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="20">

        <ThresholdFilter level="debug" onMatch="NEUTRAL" onMismatch="DENY" /> 
        <PatternLayout>
            <pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %m%n</pattern>
        </PatternLayout>
    </SMTP>

    <Async name="AsyncMailer">
        <AppenderRef ref="Mailer"/>
    </Async>
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="AsyncMailer" />
    </Root>
</Loggers>
Combust answered 15/1, 2014 at 1:49 Comment(12)
Thanks for the answer. I've just started to think that such behaviour in log4j (1.2) with the following config settings: log4j.rootLogger=ERROR, email log4j.appender.email.BufferSize=10 was a bug. Because I receive email on every error with 9 INFO events and 1 (the last event) ERROR.Galantine
Fantastic! Works as a charm! Thank you very much! I just removed level="error" from <AppenderRef ref="AsyncMailer" level="error"/> as you said and it started to work. Thanks a lot!Galantine
Thank you for your answer! It worked! Two question though.. 1. I noticed that <ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY" /> didn't filter log messages for me (logger is set to debug and I wanted the appender to include only debug). 2. I'm getting an E-Mail per log line. I expected a single file with the entire buffer. Did anyone experienced the same issue?Still
About 1: what is actually happening? 2: I think an email per log event is currently the expected behavior. But please raise a feature request on the Log4j2 JIRA!Combust
* Thanks @Remko-popma I will open a Jira. * Currently I see logs from all the log messages I logged (in debug, info and error) I expected (or wanted) to filter anything below info.Still
After changing the filter to <ThresholdFilter level="error" onMatch="ALLOW" onMismatch="DENY" /> Everything worked as I expected. A single E-Mail for an error with X line above.Still
The intention was to also provide the (trace/debug/info) events that led up to the error. But if you're happy with just the error that's ok.Combust
@RemkoPopma Is it possible to get the email containing all the logs(in the buffer) even if there was no error message throughout the execution? Currently, I get the email with previous messages only if there is atleast 1 Error level log. How to get all the messages in the email even when no error was reported in the execution?Banda
@Potatoツ Not sure I understand; what should trigger the email? Do you want an email for every log message? (I’ve seen a corporate mail system been brought down by something like this, be careful!)Combust
@RemkoPopma No, I don't need a separate email for a separate log message. Right now, I only get an email when there is an error during the execution. This email contains the error log along with all the previous logs-info/warn etc.). So far, I am happy. But, what if no error was encountered during the execution? I do not get any email in this case. I want to have an email in this case too with all the messages consolidated in that mail. Is it possible? I hope, I am making some sense. I have been looking around for quite sometime now :(Banda
@Potatoツ Sorry, it’s still not clear to me what should trigger the email.Combust
@RemkoPopma ohh. sorry for not being more clear. I have raised a question with details hereBanda

© 2022 - 2024 — McMap. All rights reserved.