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 o开发者_如何学Pythonf the tags re-encoded as <? ?>
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();
}
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?
精彩评论