Tuesday, 11 March 2014

Simple Logging - Extended

This is a followup of the Simple Logging blogpost.

It is possible to change the logging.properties file to use a FileHandler. This directs everything to a file, (obviously).

Check reference 3 for a good explanation of the logging.properties file.

Unfortunately the default values of the FileHandler in the default JSE logging.properties6 file leaves a lot to be desired.

limit5000050 kilobytes is quite small for a log file5000000
count1this means the total number of files written is 1, so if the log increases beyond 'limit', you lose the old messages as the file is re-initialized100
appendfalsemight want to set this to true, to prevent loss of log messages upon restart. Unfortunately, this means you'll have *two* XML files within the log and they need to be separated afterwards.true

The following Handlers seem to be already available in Java4, if you're looking for something different.

I very much like the XMLFormatter. It provides even more information than general logging, and it can be easily post-processed.


JLogViewer1 can view the XML logs written to file. I am sure there are other programs that perform the same function, but this is the one I happened to come across.

java -cp jlogviewer_1_0_0d.jar gianluca.utility.LogViewer

Fixing the logs

Unfortunately for me, the logs contain odd characters (because I am writing received data from sockets into the logs) that are not properly handled by jlogviewer.

I was required to remove the special characters from my logs. Found a blog post2 on how to remove them.
cat java0.log.0 | tr -d "\013" | tr -d "\001" | tr -d "\000" | tr -d "\017" > sanitized_java.log

Update 17-03-2014: updated the item on appending to existing logs.


[1] JLogViewer
[2] Removing special characters in Linux
[3] java Logging - Configuration
[4] javadoc java.util.logging.Handler
[5] javadoc java.util.logging.LogManager
[6] default logging.properties provided by JRE 7

No comments:

Post a Comment