Logging xml data in an xml log file

I am needing to log some xml data (which is currently a JDOM Document), and I am trying to output it to a standard Java log. However, this will only produce logs with the < and > ends of the tags re-encoded as &lt;? ?&gt; etc.

Although this can be parsed for the right information, it makes the log file effectively unreadable. Can anyone help with a better solution for this? I have read through a number of posts on this, but no-one seems to have come up with an answer - however, if I've missed the answer, please direct me to the right post!

Current Code: a simplified case to demonstrate what isn't working, removing the transferring of the document to XML (which can be done successfully using XMLOutputter().outputString(document)).

try {
    Logger logger = Logger.getLogger("companyname");

    FileHandler fh = new FileHandler("log.log");
    fh.setEncoding("UTF-8");

    logger.addHandler(fh);
    logger.setLevel(Level.ALL);

    String message = "<tag>Some Text</tag>";
    Logger.getLogger("companyname").log(Level.CONFIG, message);
}
catch (IOException e) {
    e.printStackTrace();
}   

Answers


It sounds like it's doing exactly the right thing - but you should be using a tool to view the log entries, rather than trying to read the log file directly. Anything which reads the log file and expects the content to be normal text content (and which may ignore unexpected tags) will behave correctly with the escaped text, but would break if the text wasn't being escaped.

To think of things another way: suppose you logged a string which happened to contain just a "<" - would you really want the log file to become an invalid XML document?


Need Your Help

If hashed password is in DB can villain use that hashed password in input box in login

javascript node.js mongodb express hash

I read that this is the module (bcrypt) to use to do login, password saving stuff. To my understanding, the main problem it solves is that if a hacker gets access to your database and you don't has...

How to send multipart binary sms in Android?

android binary sms multipart

I'm trying to send a binary sms in Android. But when the size of the message exceeds one sms, I get different errors depending on the device.