You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Barry Sheward <ba...@hypernoc.com> on 2002/05/16 15:54:23 UTC

SocketAppender and LoggingEvent

Hi,

I am getting log4j.spi.LoggingEvents via the log4j.net.SocketAppender 
and log4j.net.SimpleSocketServer which is working pretty well except I 
can't actually tell which host the messages came from. I'll set the 
scene first, and put some questions down the bottom.

I subclassed PatternParser and PatternLayout to add a new option "h" to 
display the hostname that the message originated from, and subclassed 
LoggingEvent to add a constructor which took a LoggingEvent and 
java.net.InetAddress, which a subclassed SocketNode attempts to create. 
My SocketNode.run() method looks like:

    public void run() {
        LoggingEvent event;
        Category remoteCategory;

        try {
            while(true) {   
                event = (LoggingEvent) ois.readObject();
                remoteCategory = hierarchy.getInstance(event.categoryName);
                event.category = remoteCategory;
                if ( 
event.priority.isGreaterOrEqual(remoteCategory.getChainedPriority()) ) {
                    remoteCategory.callAppenders( new LoggingEventEx( 
event, socket.getInetAddress() ) );
                }
            }
        ...

My LoggingEventEx class contains a constructor:

    public LoggingEventEx( LoggingEvent logging_evt, InetAddress 
remote_host ) {
        super( logging_evt.fqnOfCategoryClass, logging_evt.category, 
logging_evt.priority, logging_evt.getMessage(), null );
        this.remote_host = remote_host;
    }

I thought that passing null in as the ThrowableInformation parameter 
would work fine, but when I receive a message, I eventually get the 
exception:

2002-05-15 18:35:26,093 ERROR [LoggerService:Connection.66.89.196.59]  
log4j.SocketNodeEx (SocketNodeEx.java:101) - Unexpected exception. 
Closing conneciton.
java.lang.NullPointerException
        at java.lang.String.lastIndexOf(String.java:1427)
        at java.lang.String.lastIndexOf(String.java:1393)
        at org.apache.log4j.spi.LocationInfo.<init>(LocationInfo.java:116)
        at 
org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:133)
        at 
com.hypernoc.extensions.log4j.PatternParserEx$ClassNamePatternConverter.getFullyQualifiedName(PatternParserEx.java:565)
        at 
com.hypernoc.extensions.log4j.PatternParserEx$NamedPatternConverter.convert(PatternParserEx.java:538)
        at 
org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:56)
        at org.apache.log4j.PatternLayout.format(PatternLayout.java:547)
        at 
org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:295)
        at org.apache.log4j.WriterAppender.append(WriterAppender.java:157)
        at 
org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
        at 
com.hypernoc.ebx.util.ColoredConsoleAppender.doAppend(ColoredConsoleAppender.java:60)
        at 
com.hypernoc.extensions.log4j.AppenderAttachableImplEx.appendLoopOnAppenders(AppenderAttachableImplEx.java:74)
        at org.apache.log4j.Category.callAppenders(Category.java:257)
        at 
com.hypernoc.extensions.log4j.SocketNodeEx.run(SocketNodeEx.java:93)

I am running Log4J V1.1.1 at the moment. Is using null for the 
ThrowableInformation acceptable?

Is there a better way to pass this information? I tried using Nested 
Diagnostic Contexts, but we have threads being created and destroyed all 
over our code and because the NDCs are thread local, this would mean 
huge changes throughout our code.

I thought about changing the Log4J code directly, but wanted to avoid 
this in case you didn't accept my changes and I was forced to keep 
modifying Log4J as it changes, but I wondered about adding a "userData" 
field to the LoggingEvent class which could store all sorts of other 
information like this. What are your opinions?

Any advice would be appreciated.

Regards,

Barry







--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>