[slf4j-user] Best practice for logging XML and other byte oriented formats with slf4j?

Chad La Joie chad.lajoie at switch.ch
Thu Nov 19 19:18:55 CET 2009


Depending on the content of the XML you may want to consider logging the 
base64 encoded octet stream.  Trying to covert the octet stream in to a 
String is very difficult to get write and can cause all sorts of 
problems if your XML is signed or encrypted.

Thorbjoern Ravn Andersen wrote:
> Hi.
> 
> We have reached a situation where I basically want to log a data 
> structure in order to be able to process it later.
> 
> After a bit of pondering, I have concluded that the best approach for us 
> to do this would be to use the XMLEncoder/XMLDecoder in Java 1.4+ and 
> log the generated XML snippets.
> 
> The issue I want to solve is that the XMLEncoder writes an UTF-8 encoded 
> XML file to an OutputStream, i.e. a byte oriented destination.  To the 
> best of my knowledge the slf4j backends all deal with Strings, i.e. 
> character oriented destinations and the output files are written in the 
> default encoding for the platform.
> 
> The question now is, what is the best way to handle the OutputStream 
> generated by XMLEncoder so it will survive all attempts to mess up any 
> unicode characters inside due to encoding differences on the way.  I 
> will be using a custom layout anyway so much can be done :)  A humanly 
> readable transport format will be preferred.
> 
> My current thoughts is to use a ByteArrayOutputStream and generate a 
> String using the UTF-8 decoding.  The resulting string contains a <?xml 
> ... encoding="UTF-8"?> which is stripped resulting in an XML String 
> containing Unicode chars (instead of encoded bytes).  This can then be 
> flattened to an ASCII version, by converting all characters outside 
> 32..127 to their numeric entity (&#1234;), and THAT can be safely 
> logged.  I guess :)
> 
> I'd appreciate comments on my thoughts, as this is a rather important 
> intermediate step in us using log files to store information which can 
> be used to simulate an external system when replaying an interesting 
> sesion.
> 
> 

-- 
SWITCH
Serving Swiss Universities
--------------------------
Chad La Joie, Software Engineer, Net Services
Werdstrasse 2, P.O. Box, 8021 Zürich, Switzerland
phone +41 44 268 15 75, fax +41 44 268 15 68
chad.lajoie at switch.ch, http://www.switch.ch




More information about the slf4j-user mailing list