Showing posts with label loglevel. Show all posts
Showing posts with label loglevel. Show all posts

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.

property
default
comments
mine
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.

JLogViewer


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.

References

[1] JLogViewer
http://jlogviewer.sourceforge.net/
[2] Removing special characters in Linux
http://deepupc.wordpress.com/2009/10/28/removing-special-charactersmca-in-vi/
[3] java Logging - Configuration
http://tutorials.jenkov.com/java-logging/configuration.html
[4] javadoc java.util.logging.Handler
http://docs.oracle.com/javase/7/docs/api/java/util/logging/Handler.html
[5] javadoc java.util.logging.LogManager
http://docs.oracle.com/javase/7/docs/api/java/util/logging/LogManager.html
[6] default logging.properties provided by JRE 7
https://gist.github.com/maartenl/9346237

Tuesday, 21 January 2014

Simple Logging

Had a Dickens of a time, getting logging to work. I thought it would be as simple as assigning a ConsoleHandler to my logger.

Turns out that there's a Loglevel on the ConsoleHandler as well, and it is set to INFO level messages by default.

Just setting the log level on the Logger itself to Level.ALL is just not enough.

The example here is sufficient for the most basic logging of simple one-class Hello-world applications.

Of course, you can always use System.out.println statements instead for simple one-class applications, but I think it's a bad habit to get into.


Configuration


Logging configuration can be done by means of a properties file.[1][2]

The properties file can be added during the startup as VM parameters, for example -Djava.util.logging.config.file=/home/mrbear/NetBeansProjects/jtail/logging.properties.

If no properties file is provided, the default is "lib/logging.properties" in the JRE directory.[2] The default file will only show INFO messages.

The following example will show all logging all of the time.

For information on programmable logging (Class-based), see the references.

The Global Logger

To make logging even in simple cases, as easy as possible, the global logger was introduced and in Java 7 it is easily referenced.
System.out.println("x=" + x);
Can therefore be replaced by:
Logger.getGlobal().finest("x=" + x);
Unfortunately, you still have to set the LogLevel appropriately, similarly as displayed in the first code example above.

Update 2015/03/26: added the Global logger info.

References

[1] Java Logging - Configuration
http://tutorials.jenkov.com/java-logging/configuration.html
[2] LogManager - Javadoc
http://docs.oracle.com/javase/7/docs/api/java/util/logging/LogManager.html