How to send java.util.logging to log4j?

I have an existing application that does all its logging with log4j. We use a number of other libraries that either use log4j or are logged in Commons Logging, which ends up using log4j under covers in our environment. One of our dependencies even writes messages to slf4j, which also works fine, as it ultimately delegates log4j as well.

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

Not familiar with the built-in JDK log, accessible via java.util.logging, is there an easy way to have any log messages sent by JUL logged against log4j, so I can use the existing configuration and configure any log coming from ehcache?

Looking at the javadocs for the JUL, it looks like I can set up a group of environment variables to modify the LogManager implementation LogManager and possibly use it to port log4j Logger to the JUL Logger class. Is this the right approach?

The irony is that using the JDK built-in journaling library can cause such a headache when (in most cases) the rest of the world uses third-party libraries.

+82
java apache-commons logging slf4j log4j
May 15 '09 at 17:31
source share
7 answers

One approach that I have used successfully is to use slf4j as my main logging API. Then I bind slf4j to log4j. Third party dependencies using other frameworks (e.g. JUL) can be connected to slf4j.

+36
May 15 '09 at 17:47
source share

We use SLF4J in our current project, and it worked very well for us. SLF4J was written by Ceki Gülcü, the creator of Log4J, and it did a great job. In our code, we directly use the SLF4J APIs, and we configure SLF4J to make calls from Jakarta Commons Logging (JCL), java.util. (JUL) and Log4J APIs are fully connected to the SLF4J APIs. We must do this because, like you, we use third-party libraries (open source) that have chosen different logging APIs.

At the bottom of SLF4J, you configure it to use a particular registrar implementation. It comes with an internal or “simple” logger, and you can override this with Log4J, JUL, or Logback . Configuration is done simply by deleting various jar files in your class path.

We originally used the Logback implementation, also written by Ceki Gülcü. It is very powerful. However, then we decided to deploy our application on the Java EE Glassfish application server, whose log viewer is waiting for messages in JUL format. Thus, today I switched from Logback to JUL, and in just a few minutes I replaced two log banks with the SLF4J tank, which connects it to the JUL implementation.

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

+18
May 16 '09 at 6:38
source share

There is a simpler alternative than SLF4J for connecting JUL with log4j, see http://people.apache.org/~psmith/logging.apache.org/sandbox/jul-log4j-bridge/examples.html

You just need to put jul-log4j-bridge in the classpath and add the system property:

 -Djava.util.logging.manager=org.apache.logging.julbridge.JULBridgeLogManager 

jul-log4j-bridge is not located in Maven Central and can be extracted from this repository:

 <repository> <id>psmith</id> <url>http://people.apache.org/~psmith/logging.apache.org/repo</url> <releases> <enabled>false</enabled> </releases> </repository> 

and then used with:

 <dependency> <groupId>org.apache.logging</groupId> <artifactId>apache-jul-log4j-bridge</artifactId> <version>1.0.0-SNAPSHOT</version> <scope>test</scope> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>apache-log4j-component</artifactId> </exclusion> </exclusions> </dependency> 

You can also rebuild it from sources with the following steps:

+12
Nov 30 '10 at 21:02
source share

OCTOBER 2014

Since in version 2.1 log4j there is a component log4j-jul, which allows just that. However, if you use log4j 1, you must upgrade to log4j2 to use this approach.

JDK Logging Adapter

Class logmanager

Migrate from log4j 1.x to log4j 2

+6
Oct 24 '14 at 10:50
source share

My slf4j site has a bridge for passing java.util.logging events through slf4j (and therefore log4j).

Yes, loading SLF4J contains jul-to-slf4j, which I believe does just that. It contains a JUL handler for passing records to SLF4J.

+3
May 15 '09 at 17:48
source share

@Yishai - Thanks for posting a link to my wiki. In the example, JUL is redirected to Log4J, and I have been working in the production system for several years. JBoss 5.x is already redirecting JUL to Log4J, so I took it when we updated it. I have a newer one that redirects to SLF4J, which I am currently using now. I will send it when I have a chance.

However, SLF4J already has it:

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

+2
Jun 21 '10 at 17:36
source share

you must manually add blows at startup

 SLF4JBridgeHandler.removeHandlersForRootLogger() SLF4JBridgeHandler.install() 

demo → https://gist.github.com/jiahut/654ecc75a13b0a1d8f3b4d5d2d69dc6d

0
Aug 09 '18 at 10:00
source share



All Articles