I recognized some problems with the SMTPAppender in log4j2. Whenever log events with the level error
or fatal
are created without having an event with the level info
before no mail is sent and the fatal event disappears.
Here is my log4j2 configuration file (log4j2.xml) and a small program (LogTest.java) to reproduce the problem:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">
<!-- mail server configuration -->
<properties>
<property name="receipients">[email protected]</property>
<property name="from">[email protected]</property>
<property name="smtpHost">smtp.example.com</property>
<property name="smtpPort">25</property>
<property name="smtpProtocol">smtp</property>
<property name="smtpUser">me</property>
<property name="smtpPassword">secret</property>
</properties>
<appenders>
<!-- appender to write all info events to stdout -->
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY"/>
</Console>
<!-- appender to send mails (default: error and fatal events)-->
<SMTP name="Mailer" suppressExceptions="false"
subject="Error log" to="${receipients}" from="${from}"
smtpHost="${smtpHost}" smtpPort="${smtpPort}"
smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="2">
</SMTP>
<!-- appender to send mails asynchronously -->
<Async name="AsyncMailer" >
<appender-ref ref="Mailer"/>
</Async>
</appenders>
<loggers>
<!-- logger to send mail on (at least) info level events -->
<logger name="LogTest" level="info" additivity="true">
<appender-ref ref="AsyncMailer"/>
</logger>
<!-- root logger to see what happens (info level and "above") -->
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I used this small program to reproduce the problem (LogTest.java):
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
class LogTest
{
private static Logger logger=LogManager.getLogger("LogTest");
public void testlogger()
{
/* --> uncomment to enable first mail
logger.info("test info 1");
*/
logger.fatal("test fatal 1");
/* --> uncomment to enable second mail
logger.info("test info 2");
*/
logger.fatal("test fatal 2");
}
public static void main(String[] args)
{
LogTest app=new LogTest();
app.testlogger();
}
}
If you uncomment the two marked positions everything work like intended: two mails are sent - each containing the fatal-event and the prior info event. Additionally the 4 events are printed to stdout:
test info 1
test fatal 1
test info 2
test fatal 2
Now, if you only activate/uncomment the second position - the second mail (fatal2) is sent as intended (again with the prior info2 event), but even though the first fatal event is printed to stdout the mail is eaten up. The output looks as follows:
test fatal 1
test info 2
test fatal 2
Personally, for me it seems like I got something wrong and mis-configured log4j2 or it might be a bug.
Thanks for your help in advance.
*Jost
Note:
For the tests I used log4j2-beta7 downloaded from the project's website. The documentation can be found here.