Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-488

SMTPAppender Does Not Log Below Level "Error"

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.0-beta9
    • None
    • Appenders
    • None
    • Tomcat, Servlet 3.0

    Description

      SMTPAppender attempts to connect to SMTP server only when logging at level Error or Fatal, regardless of the level defined in Log4J2.xml.

      This bug sounds similar to LOG4J2-310, "SMTPAppender does not send mails with error or fatal level without prior info event," but it is different.

      Steps to reproduce:

      This code:

      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      
      class LogTest {
      	private static Logger logger = LogManager.getLogger("LogTest");
      
      	public void testlogger() {
      		logger.info("test info"); // does not log
      		logger.error("test error"); // logs!
      	}
      
      	public static void main(String[] args) {
      		LogTest app = new LogTest();
      		app.testlogger();
      	}
      }
      

      With this configuration file:

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration status="warn">
      	<!-- Mail server configuration. You don't need to change these to see the 
      		problem with SMTPAppender. Because smtpDebug is set to "true" you'll see 
      		that the appender makes no attempt whatsoever to connect to an SMTP server 
      		while logging at any level other than fatal or error, thus demonstrating 
      		the bug. -->
      	<properties>
      		<property name="receipients">me@example.com</property>
      		<property name="from">me@example.com</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" ignoreExceptions="false" subject="Error log"
      			to="${receipients}" from="${from}" smtpHost="${smtpHost}" smtpPort="${smtpPort}"
      			smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
      			smtpPassword="${smtpPassword}" smtpDebug="true" 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>
      

      Demonstrates the problem.

      Attachments

        1. log4j2-smtp.xml
          2 kB
          Matt Sicker
        2. SMTPAppenderTest.java
          8 kB
          Matt Sicker

        Activity

          People

            Unassigned Unassigned
            mbmast Michael Mast
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: