Log statements help record the flow of an application’s execution. Hence they are an important part of software coding process. For the Java language the common Log API libraries are Java Util Logger (commonly known as JUL), Apache Log4J, SLF4J etc. While these libraries support many advanced capabilities, most developers only use a handful of features. For example, ability to send log messages to files and the ability change log levels dynamically etc.
In this blog I will compare JUL against SLF4J and focus on these above mentioned features. Many will argue that this is not a fair comparison since SFL4J is really a facade and supports JUL as well. To clarify, I will compare the SimpleLogger that is bundled with SLF4J against direct JUL usage.
In order to setup SLF4J with SimpleLogger you just need to include both JAR files in your class path. Please refer to the SLF4J Manual.
Log API - Logging to Console
By default most Log API libraries support redirecting output to console. Let us look at a code snippet that shows how this is accomplished in JUL and SLF4J.
The output of the above program is as follows:
Oct 06, 2013 11:05:29 PM LoggerComparison1 main ==> This is printed by JUL SEVERE: This message is from Java Util Logger ==> This is printed by JUL [main] WARN LoggerComparison1 - This message is from SLF4J Logger ==> This is printed by SLF4J
- You can see that Java Util Logger did not print the “finer” statement. This is because the default Log level is INFO
- The more interesting thing to observe is that JUL prints two lines of logger output. This can be confusing while doing GREP.
- The SLF4J Simple Logger does not print the time stamp by default. I strongly believe that most systematic users will end up configuring SLF4J such that timestamp is printed.
- Overall, both JUL and SLF4J have some drawbacks while use them in their minimally initialized avatars.
Log API - including timestamp and changing log level
Let us see how we can include timestamp for SLF4J Simple Logger (JUL already includes it) and also check out how log levels can be changed.
If you compile and run the above program, here is the output you get:
Oct 20, 2013 10:35:27 PM LoggerComparison2 main SEVERE: This SEVERE Log is from Java Util Logger. 20-10-13 22:35:27 [main] WARN LoggerComparison2 - This WARN Log is from SLF4J.
- You can see that the timestamp limitation of SLF4J Simple Logger can be overcome very easily. In fact SLF4J Simple Logger uses system property to customize the logger and hence can be configured in many different ways with ease.
- Also note that unlike JUL, the timestamp of SLF4J Simple Logger is included in the same line as the log statement output.
- You can also see that by setting appropriate Log levels, we can include or exclude some statements from the output.
Log API - Redirecting output to a file
Finally, lets look at one more commonly used log API feature - redirecting output to a file. Here is the program:
If you compile and run the above, you will notice several interesting results.
- It is much easier to configure output redirection in SLF4J vs. Java Logger. In SLF4J you simply set the appropriate property whereas in Java Logger you need to create an instance of FileHandler.
- Even though Java Logger is configured with FileHandler, a copy of the log statements is redirected to Console also. This may not be the desired result everytime. This is as per design of Java Logger where multiple handlers are supported. If you want to have only one Handler in JUL, you need to write additional code.
- Probably the most peculiar result of this program, is that the output file for JUL contains XML message where as SLF4J output file contains the logger output as was seen on the console earlier.
- This is yet again an unwanted design intent of Java Logger. Each Handler can have a Formatter associated with it. And the output format is controlled by the Formatter. For some weird reason, the FileHandler of JUL uses an XMLFormatter by default.
Log API - Conclusion
With these three simple programs, it can be seen that with SLF4J you can hit the ground running. For the most common and also the most used Log API features, SLF4J (Simple Logger) provides much simple APIs when compared to Java Util Logger.