logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Spitzer <aspit...@gmail.com>
Subject Re: How can I write a custom appender to supports Throwable as a second parameter?
Date Mon, 28 Sep 2009 17:35:42 GMT
log4j does not appear to append the stack trace onto the event before
passing it onto my appender.  Here is a quick test I just did:

--CUT--- MyAppender.java - A CUSTOM APPENDER-----------------
import java.net.*;
import java.util.*;
import java.io.*;
import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import org.apache.log4j.helpers.LogLog;

public class MyAppender extends AppenderSkeleton
{
    private Layout forcedLayout;
    private boolean init=false;
    public MyAppender(){}
    public boolean requiresLayout(){return false;}
    public void close(){}
    /************************/
    private void init_vars()
    {
        forcedLayout=new PatternLayout("%p %c %m");
        init=true;
    }
    /************************/
    protected void append(LoggingEvent event)
    {
        if (! init)
            init_vars();

        System.out.println("JUST TESTIN: "+ forcedLayout.format(event));
    }
    /************************/
}


--CUT--- MyAppender.java - A CUSTOM APPENDER-----------------

---CUT --- MyTest..java ---
import org.apache.log4j.*;

public class MyTest
{
    public static void main(String[] args) throws Exception
    {
        Logger logger=Logger.getLogger(MyTest.class.getName());
        BasicConfigurator.configure();
        logger.debug("hi");

        String[] bad=new String[1];
        try
            {
                bad[2]="bomb";
            }
        catch(Exception e)
            {
                logger.fatal("hopefully there is a stack trace:",e);
            }
    }
}
---CUT --- MyTest..java ---

--CUT --- log4j.properties ----
log4j.rootLogger=DEBUG, MyAppender
log4j.appender.MyAppender=MyAppender
--CUT --- log4j.properties ----


OUTPUT of run:
13:24:06 aspitzer@alex:~> java -cp log4j-1.2.9.jar:. MyTest

JUST TESTIN: DEBUG MyTest hi
0 [main] DEBUG MyTest  - hi
JUST TESTIN: FATAL MyTest hopefully there is a stack trace:
7 [main] FATAL MyTest  - hopefully there is a stack trace:
java.lang.ArrayIndexOutOfBoundsException: 2
        at MyTest.main(MyTest.java:14)

You can see that my "JUST TESTIN" appender only shows the string, and
not the exception stack trace.

-alex spitzer

Cell: 617.407.2274
AIM: AlexSpitzer




On Mon, Sep 28, 2009 at 12:51 PM, Simon Gibbs <simon.gibbs@cantorva.com> wrote:
> Alexander Spitzer wrote:
>> if they use
>> logger.error("this is an error",myException)
>>
> .. then the exception is added to the event and passed to the appender
> as 1 parameter.
>
> perhaps the appender is going wrong when dealing with this data, try
> debugging the appender. The logger code is right (and is not subject to
> change anyway).
>
> logger != appender  :-)
>
> Simon
>
>

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


Mime
View raw message