The Java log levels showdown: SEVERE FATAL ERROR OMG PANIC
Capitalized log levels induce high levels of stress. What if, instead of ERROR we’d just use “oops”? On a more serious note, we’ve recently ran a huge data crunch over GitHub’s top Java projects and the logging statements they use , revealing the log level breakdown of the average Java project.
In this post, we’ll explore the resulting data set from another angle, shed some more light on the dataset, and put the focus on the use of standard java.util.logging levels versus more popular frameworks like Log4j (+ Log4j 2), and Logback.
Step right in.
New Post: Is Standard Java Logging Dead? Log4j vs Log4j2 vs Logback vs java.util.logging https://t.co/cxIOCe0gWd pic.twitter.com/BmKIUIIDb4
— OverOps (@overopshq) November 29, 2016
Meet the players
Logging utilities can be roughly divided to 2 categories: the logging facade and the logging engine.
As far as logging facades go, you pretty much have 2 choices: slf4j and Apache’s commons-logging. In practice, 4 out of 5 Java projects choose to go with slf4j . Based on data from the top Java libraries in 2016 on Github . The motivation for using a logging facade is pretty definitive and straightforward, an abstraction on top of your logging engine of choice – allowing you to replace it without changing the actual code and logging statements.
As to the logging engine, the most popular picks are Logback, which is an evolved version of Log4j , Log4j itself, and its new version since the development was passed on to the Apache Software Foundation, Log4j2. Trailing behind is Java’s default logging engine, java.util.logging aka JUL.
Pointing fingers and calling names
On the “superficial” side of things, each of the logging frameworks has slightly different names for their logging levels.
In the rare case where slf4j is used with java.util.logging, the following mapping takes place:
FINEST -> TRACE
FINER -> DEBUG
FINE -> DEBUG
INFO -> INFO
WARNING -> WARN
SEVERE -> ERROR
Another thing to notice here is that Logback and java.util.logging have no FATAL equivalent. Behind those error names, are simple integer values, that help control the logging level in a running applications. Each library also contains values for OFF and ALL, which basically set the logger level to actually transmit everything, or nothing. Setting a logger level at WARN for instance, would only log WARN messages and above – Its practically the default setting for production environments.
btw, one of the cool things about the tool that we’re building , is that you can get log messages lower than WARN in production, even if you’ve set the logger level to WARN . Check out this video for a quick (25 sec) demonstration .
How does the level naming breakdown look in practice?
For the data crunch, we focused on the top starred Java projects with at least 100 logging statements in either of the methods. Examining the data set of projects, here’s what we found: