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 Ku...@equifax.com on 2005/09/01 19:52:00 UTC

Serializable NDC DiagnosticContext




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;
      }
    }
  }


Thanks,

-Kurt Eckhardt
This message contains information from Equifax Inc. which may be
confidential and privileged.  If you are not an intended recipient, please
refrain from any disclosure, copying, distribution or use of this
information and note that such actions are prohibited.  If you have
received this transmission in error, please notify by e-mail
postmaster@equifax.com.


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


Re: Serializable NDC DiagnosticContext

Posted by Curt Arnold <ca...@apache.org>.
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