logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Serializable NDC DiagnosticContext
Date Thu, 01 Sep 2005 20:55:09 GMT

On Sep 1, 2005, at 12:52 PM, Kurt.Eckhardt@equifax.com wrote:
> Hi Folks,
>
> I'd like to pass a cloned NDC Stack across an EJB call, but can't  
> since its
> NDC.DiagnosticContext entries are not serializable.  Does anybody  
> see any
> potential issues with adding an "implements java.io.Serializable"  
> to the
> DiagnosticContext class (see snip below)?
>
> //  
> =====================================================================
>    private static class DiagnosticContext implements  
> java.io.Serializable {
>
>     String fullMessage;
>     String message;
>
>     DiagnosticContext(String message, DiagnosticContext parent) {
>       this.message = message;
>       if(parent != null) {
>       fullMessage = parent.fullMessage + ' ' + message;
>       } else {
>       fullMessage = message;
>       }
>     }
>   }
>

I have a couple of problems with the suggested implementation:

DiagnosticContext is a private inner class and not part of the  
exposed log4j API.  As it is currently, the NDC implementation could  
be rewritten to eliminate use of DiagnosticContext without breaking  
the API.

Using the default serialized form is undesirable.  See Item 55 in  
"Effective Java" for an explanation.

Serializing a java.util.Stack (or any java.util.Collection) can be  
wasteful.  Again see Item 55.

The best thing I could suggest would be to implement  
DiagnosticContext.toString() returning fullMessage.  That way the  
class that you using to perform serialization could do something like:


void writeObject(ObjectOutputStream s) {
...
...
Stack clonedNDC = NDC.cloneStack();
String[] ndcContents = null;
if (clonedNDC != null) {
     ndcContents = new String[clonedNDC.size()];
     for (int i = 0; i < ndcContents.length; i++) {
          ndcContents[i] = clonedNDC.get(i).toString();
     }
}
s.writeObject(ndcContents);
}

and the corresponding readObject() could rebuild the NDC if desired.

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


Mime
View raw message