Shouldn’t we Standardize a Java Logging API?
If you are interested in Java EE development and the roadmap, you might have read recently that the Cloud feature in Java EE 7 has been delayed. As I’ve already expressed in the Java EE 7 expert group mailing list,
I’m happy about this news because I feel standardizing cloud in EE 7
was way too early. But I’m also sad. Sad because we’ve spent a lot of
time discussing Cloud, PaaS, multi-tenancy… and not concentrating on
other topics. And one topic that I have in mind is logging! Shouldn’t we standardize a Logging API ?
A few months ago I was struggling (again) with logging configuration in
JBoss 7.x. I was so depressed that I wrote to the Java EE 7 expert
group : Logs. Should we finally do something ? (you
should read it, there are valuable opinions expressed by the
members). As a developer I’ve used all the possible logging Java
frameworks for the last 12 years and I still struggle with logging in
2012. How many logging frameworks do we have ? Look at the list :
- JUL : the good old java.util.logging API that is included in our JDK. Bad luck, it is there and nobody uses it
- Log4j : widely used, Log4j hasn’t really been updated lately (update : the team is working on a 2.0 version)
- Commons Logging : How many more Apache logging frameworks do you want ?
- SLF4J : if you are not sure which logging API to use, well, use an abstraction (hum, abstraction, we love abstraction)
- Logback : the successor of Log4J (successor ? really ?)
- TinyLog : because all these frameworks are too big, we need to use a tiny one
- And many many many more Java logging frameworks
I’m not a log expert and I’m sure all these frameworks exist for good reasons (I’m trying to be politically correct here), but for god sake, we just need to write logs. I don’t want to do a benchmark of all the logging APIs to make up my mind. I don’t want to have to relearn a new logging framework each time a new version of my application server is out (yes, JBoss 7 uses its own JBoss Logging API). To be honest, instead of all saying “using System.out.println is bad” we should have encouraged it and let Java SE redirect the messages to files (using external configuration). This way our code would be full of System.out.println("message") or System.out.println(WARNING, "message") and we would have avoided all the logging frameworks. Anyway, this is the situation we’re in.
Let’s standardize a logging API
Standardizing too soon is bad… but we’ve been doing logs for more than a decade now. We have a big expertise in logging, we could even take some ideas of other languages. So it’s time to standardize logging. Let’s not do the same mistake as JUL and only standardize an API. Then, we could let all the known frameworks to implement it and fight about better performances or functionalities. Today, logging portability is a nightmare, enough. With a standard API we could also have some tooling and descent log viewers coming on board.
Integrate this logging API in Java EE (and other specs)
If all the Java EE specs (EJB, JPA, JAX-RS, CDI…) use this standard API and impose it to the implementations, we could then easily change the level of logs in a standard way on our applications by using a simple package naming convetion:
javax.ejb.level = debug // EJB spec javax.persistence.level = debug // JPA spec org.weld.level = debug // CDI implementation (Weld) my.app.level = debug // My application
And who knows, when modularization arrives we could get rid of java.util.logging and have a new javax.logging module in Java SE (and choose the implementation).
Think to the future
If we had a Logging API 1.0 specification, then, when multi-tenancy comes along, we could have a Logging API 1.1 which handles it (wouldn’t it be nice to have multi-tenant logs with no effort ?). Another idea would be to play with staging. JSF 2.0 introduced staging and it might be spread all over Java EE. If that’s the case, we could simply use default logging : DEBUG for development, WARNING for production… That would simplify configuration.
Do you feel like being a spec lead ?
No, it’s not because I write this blog post that I want to be a spec lead. I’m not the right person, I’ve never implemented a logging framework, I’m just a user of theses APIs. So if you feel you know the topic well, if you know people who could help you by being part of your expert group (if you want, I could even join this expert group), then do it. Entering the JCP is not a piece of cake, you need to do a bit of administration to have your JSR approved, but it’s doable. You would need a bit of time, but it’s doable. Even if you start now I’m not sure there will be enough time to take the Java EE 7 train, but the JCP has some examples of quick JSRs.
So, who wants to be a Logging API Spec Lead ? Do you think it’s a good idea to standardize a Logging API ?
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
John J. Franey replied on Fri, 2012/09/07 - 8:57am
You are looking for an API, mention one, and fail to recognize it as an API. It is right under your nose.
Let it lie.
Michele Mauro replied on Fri, 2012/09/07 - 9:51am
Just one url: http://xkcd.com/927/
The other comment says it all.
Mirkus Mirkelinos replied on Fri, 2012/09/07 - 9:51am
in response to:
John J. Franey
Jonathan Fisher replied on Fri, 2012/09/07 - 11:18am
The community is slowing standardizing SLF4J IMHO. It's the API the author is looking for.
SLF4J is nothing but a vanilla API and set of interfaces. This author assumes it's an implementation. It's literally an API, nothing else.
Logback implements those interfaces. So it is _no way_ a successor, it's an *implementation*. SLF4J is useless without implementations, and Logback is useless without an API. There are 5-6 implementations of SLF4J, which is cool. I like choice :)
This is awesome for API designers/library inventors. Then can write their code with contextual log statements, and the consumer of their library can choose to ignore or add an implementation of those log statements later.
Honestly, if it isn't too much trouble, I'd ask the author to publish a correction; I'd hate for someone to make an assumption that Logback is a successor.
Nicolas Seyvet replied on Fri, 2012/09/07 - 11:20am
in response to:
Jonathan Fisher
I get the point of the author. Even if SLF4J is the de-facto standard it is not recognized as such because it is not a JSR API. It would simple to say: "OK, SLF4J is the standard. It is used as such -> JSR defined". End of conversation about which logging FW to use when developping SW.
John J. Franey replied on Fri, 2012/09/07 - 11:47am
in response to:
Nicolas Seyvet
@Nicolas The JSR for a logging api was released in 2001. It did not end the conversation.
Gregory Smith replied on Fri, 2012/09/07 - 2:24pm
My preference would be to have a "log" method on the Object class. Every object potentially needs logging - most will use logging at one time or another.
It could work the same way that JUL and log4j do, but natively as part of the Object class. Configuring the logger and appender could be done declaritively or programmatically with sane defaults just like log4j and JUL. Additional "trc", "dbg", "cfg", "inf", "wrn", "err", and "ftl" methods could be available as shortcuts for logging at particular log levels.
The JVM could be smart enough to recognise the calls to log methods and skip them entirely if they are below the log level (avoiding unneeded method calls).
Of course, it is a little late to be adding it to Java now as it would likely cause problems with existing code. Maybe a new language like Kotlin could add this feature.
Nicolas Seyvet replied on Fri, 2012/09/07 - 4:08pm
in response to:
John J. Franey
@John
Really? Ouppss... Never heard of it
Thibault Delor replied on Sat, 2012/09/08 - 7:12am
Hi Antonio,
The JSR already exists : JSR-47. Making an other JSR wouldn't have any sense.
The problem is not to have a JSR but how to make everyone use this spec...
Dapeng Liu replied on Mon, 2012/09/10 - 2:31am
Joern Haferstroh replied on Tue, 2012/09/11 - 4:10am