You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Scott Heaberlin <he...@yahoo.com> on 2003/04/30 20:31:06 UTC

Log4j possible SMTPAppender (or configuration) problem

Hello all, I have an interesting problem:

In my log4j.xml file, I define a root logger with a
single appender (a custom appender).  I configure that
appender to use a FallbackErrorHandler, which
references an appender named "fallback" (a
FileAppender).

In my custom appender (extends AppenderSkeleton),
whenever an exception is caught, I make a call to
errorHandler.error(...).  This works as expected, as
Log4J close()s the original appender and all
subsequent log events of the root logger use the
"fallback" appender.  Works like a charm.

Here's the problem:
I then received the requirement that an e-mail should
go to an internal e-mail address when our custom
appender was unable to log (as this usually happens
when the system it uses is misconfigured).  To achieve
this, I created a non-additive logger declaration in
my log4j.xml file whose name was the fqcn of our
custom appender class.  Within this class I declared
an internal Logger object (Logger.getLogger()), and
just before the call to errorHandler.error(...) I
added a log.error() call using the internal Logger
object.  The purpose for this was so I could send
error output from our custom appender to some other
Log4J appender, namely the SMTPAppender.  I tested
this code using a FileAppender instead of the
SMTPAppender, and everything worked as planned - a
single output to the internal specified Logger when
the root logger's main (only) appender failed.  The
root logger then fell-back to the fallback appender
and no logging was lost.
When I changed my log4j config file to use the
SMTPAppender, suddenly *no* appenders were created,
neither for the internal logger I specified *nor* the
root logger.  No messages were printed to System.err
until the rest of the application attempted to log,
then of course the "Warning- no appender found for
<classname>. Please initialize..." message we are all
familiar with from when we first started using Log4j.
Why would switching the config file to the
SMTPAppender cause all log4j functions to fail to
startup?

Some system background:
mail.jar, smtp.jar and mailapi.jar are all in the same
directory as log4j-1.2.8.jar are, and are loaded by
the same classloader.
The SMTP server is functioning correctly and my params
for the appender are valid - log4j never attempts to
send via smtp though; it appears to be stopping prior
to this point.  I have verified on my test box using
Ethereal that no SMTP traffic is being sent.
We were using JavaMail 1.2, but I have tested with
JavaMail 1.3 and get the same results.

Here is my log4j.xml file.  Note the commented out
FileAppender with the same name as the SMTPAppender. 
Uncommenting the FileAppender and commenting out the
SMTPAppender causes the app to work as expected.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration
xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="CustomAppender"
class="OurCustomAppenderClass">
      <errorHandler
class="org.apache.log4j.varia.FallbackErrorHandler">
        <root-ref/>
        <appender-ref ref="fallback" />
      </errorHandler>      
      <!-- some <param> elements here for our appender
-->
   </appender>
   
  <appender name="fallback"
class="org.apache.log4j.FileAppender">
     <param name="File"
value="d:/logs/log4j/fallback.xml" />
     <layout class="org.apache.log4j.xml.XMLLayout"/>
  </appender>   
  
  <!--
  <appender name="internal_error"
class="org.apache.log4j.FileAppender">
     <param name="File"
value="d:/logs/log4j/internal_error.xml" />
     <layout class="org.apache.log4j.xml.XMLLayout"/>
  </appender>     
  -->
  
  <appender name="internal_error"
class="org.apache.log4j.net.SMTPAppender">
    <param name="From" value="noreply@ourdomain.com"
/>
    <param name="To" value="toAddress@ourdomain.com"
/>
    <param name="Subject" value="**internal logging
failure**" />
    <param name="SMTPHost"
value="ourmailhost.ourdomain.com" />
    <param name="BufferSize" value="1"/>
    <param name="Threshold" value="ERROR" />
    <layout class="org.apache.log4j.PatternLayout" >
        <param name="ConversionPattern"
value="=[%d{HH:mm:ss.SSS}] %n server: %X{hostname} %n
ip: (%X{ip}) %n %n %m" />
    </layout>
  </appender>    
  
  
    <logger name="ourCustomAppenderClass"
additivity="false">
        <level value="ERROR"/>
        <appender-ref ref="internal_error"/>
    </logger>  
  
   <root>
      <appender-ref ref="CustomAppender"/>
   </root>
   
</log4j:configuration>


Here is the error handling code from our custom
appender:

    private void handleError(String message, Exception
exception, LoggingEvent event){
        //LogLog.setInternalDebugging(true);
        //LogLog.setQuietMode(false);
        _internalLogger.error(message);
        this.errorHandler.error(message, exception, 0,
event);        
    }

*note the commented out LogLog setters - I added these
to see if log4j was erroring internally, but again got
no output to standard err.

Any suggestions appreciated.


-Scott Heaberlin

__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org