logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bender Heri" <HBen...@Ergonomics.ch>
Subject Re: How to log binary data
Date Mon, 17 Mar 2008 14:11:32 GMT
I for myself do such things when preparing the log message, i.e.:

if (myLog.isDebugEnabled())
{
    myLog.debug( "received binary data:\n" + Conversions.byteArrayToHexDump( myByteArray )
);
}

Annother apporach is to write your own renderer. In this case you would have also to define
an own class which transports the message and binary data through the log4j layers until it
reaches your renderer.

Configuration:

    <renderer renderedClass="my.company.com.BinaryWrapper"
        renderingClass="my.company.com.BinaryRenderer"/>

Code:
if (myLog.isDebugEnabled())
{
    BinaryWrapper wrap = new BinaryWrapper( "received binary data", myByteArray );
    myLog.debug( wrap );
}

When log4j is about to convert the supplied Object to String, it will call your BinaryRenderer
supplying your BinaryWrapper instance. Remember: the formal parameter of Logger.debug etc.
is an Object and not a String. If no renderer for a particular class is configured, log4j
just calls toString() of the supplied Object otherwise it calls the configured renderer.

Heri


> -----Original Message-----
> From: news [mailto:news@ger.gmane.org]On Behalf Of teknokrat
> Sent: Monday, March 17, 2008 2:06 PM
> To: log4j-user@logging.apache.org
> Subject: [SPAM (Bayesain Analysis)] - How to log binary data 
> - Bayesian
> Filter detected spam
> 
> 
> I have a requirement to log raw binary data. This will be 
> mostly ASCII 
> characters but will contain non-displayable control 
> characters such as 
> \0, etc. My first thought was to write an appender by extending the 
> FileAppender class but I cannot see how to do this as I need 
> access to 
> an OutputStream and not a Writer. Does anyone have any good 
> ideas of how 
> this can be done witht he minimum of fuss in log4j?
> 
> cheers
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 

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