views:

3819

answers:

7

I have an existing application which does all of its logging against log4j. We use a number of other libraries that either also use log4j, or log against Commons Logging, which ends up using log4j under the covers in our environment. One of our dependencies even logs against slf4j, which also works fine since it eventually delegates to log4j as well.

Now, I'd like to add ehcache to this application for some caching needs. Previous versions of ehcache used commons-logging, which would have worked perfectly in this scenario, but as of version 1.6-beta1 they have removed the dependency on commons-logging and replaced it with java.util.logging instead.

Not really being familiar with the built-in JDK logging available with java.util.logging, is there an easy way to have any log messages sent to JUL logged against log4j, so I can use my existing configuration and set up for any logging coming from ehcache?

Looking at the javadocs for JUL, it looks like I could set up a bunch of environment variables to change which LogManager implementation is used, and perhaps use that to wrap log4j Loggers in the JUL Logger class. Is this the correct approach?

Kind of ironic that a library's use of built-in JDK logging would cause such a headache when (most of) the rest of the world is using 3rd party libraries instead.

+2  A: 

Here is someone who already did it. Never tried it, though.

Yishai
SLF4J already has this: http://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j
Joshua Davis
+12  A: 

One approach I have used successfully is to use slf4j as my primary logging API. I then have slf4j bind to log4j. 3rd party dependencies using other frameworks (like JUL) can be bridged to slf4j.

overthink
Good link, but I think you meant #jul-to-slf4j
araqnid
Good catch. I've updated the answer accordingly. Thanks!
overthink
This sounds like a good approach, except I can't seem to get it to work :(
matt b
Finally got this to work - was causing some big headaches until I finally discovered/realized that Tomcat supplies it's own JUL implementation and logging, and that I would need to add some sort of root logging level to it's logging properties in order for the bridge to even work. Jesus Christ JUL sucks!
matt b
Also, I can't believe that a library as popular as ehcache would make a switch to something like java.util.logging - seems very boneheaded
matt b
Yeah, it's incredibly frustrating to have to fight with this stuff all time time (well, each time you start some new project, it seems). I'm not sure of ehcache's motivation for the switch. The main benefit I can see of JUL is that it's not another dependency... even if it does suck :)
overthink
@matt b, JUL is present in the Java runtime always so it requires the least external dependencies. It is, however, in my eyes a true example of code written by people not experienced with the usages of that code. The configuration system is rather inconvenient.
Thorbjørn Ravn Andersen
+3  A: 

The slf4j site I believe has a bridge for passing java.util.logging events via slf4j (and hence to log4j).

Yes, the SLF4J download contains jul-to-slf4j which I believe does just that. It contains a JUL handler to pass records to SLF4J.

araqnid
http://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j
Joshua Davis
A: 

And just for reference, here is a class that redirects jul to common logging.

Onots
+5  A: 

We use SLF4J on our current project and it's worked very well for us. SLF4J is written by Ceki Gülcü, the creator of Log4J, and he's done a really great job. In our code we use the SLF4J logging APIs directly, and we configure SLF4J so that calls from the Jakarta Commons Logging (JCL), java.util.logging (JUL), and Log4J APIs are all bridged to the SLF4J APIs. We need to do that because like you we use third party (open source) libraries that have chosen different logging APIs.

On the bottom of SLF4J, you configure it to use a particular logger implementation. It comes with an internal, or "simple" logger, and you can override this with Log4J, JUL, or Logback. Configuration is all done simply by dropping in different jar files in your classpath.

Originally, we used the Logback implementation, also written by Ceki Gülcü. This is very powerful. However, we then decided to deploy our application to the Glassfish Java EE application server, whose log viewer expects JUL-formatted messages. So just today I switched from Logback to JUL, and in just a few minutes I replaced two Logback jars with an SLF4J jar that connects it to the JUL implementation.

So like @overthink, I would heartily recommend using SLF4J in your setup.

Jim Ferrans
How many times does Ceki need to reinvent a logging framework/fascade ?
mP
@mP: Logging may not be glamorous, but it's a crucial need for large-scale commercial-grade software. And SLF4J solves the problem of integrating code that uses disparate logging frameworks (made more urgent by Sun electing to develop java.utils.logging instead of adopting Log4J).
Jim Ferrans
@mP, slf4j was necessary because the bad job Sun did with JUL. Logback is a fork of log4j, not a new project.
Thorbjørn Ravn Andersen
A: 

Thanks for this post, very useful. I still have a small problem though:

My current project uses log4j "directly", to log into different files. But one of the project dependencies uses JUL, and is logging to the console. So as I want all my logs to go to a file, I started using the jul-to-slf4j bridge and the log4j implementation of slf4j.

This way everything is logged into files using my log4j configuration:

  • the project logs are still going directly to files
  • the logs from the dependency (the JUL logs) are routed to slf4j which actually uses log4j.

JUL logs are logged into the expected files, but the only problem is that they are also still logged to the console, and I'd like to avoid this.

Any idea on how to do this?

PhilF
+1  A: 

@Yishai - Thanks for posting the link to my wiki. The example there redirects JUL to Log4J and I've had it running in a production system for a few years. JBoss 5.x already redirects JUL to Log4J, so I took it out when we upgraded. I have a newer one that redirects to SLF4J, which I use on a few things now. I'll post that when I get a chance.

However, SLF4J already has it:

http://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j

Joshua Davis