restlet / restlet-framework-java

The first REST API framework for Java
https://restlet.talend.com
Other
653 stars 284 forks source link

restlet logging using log4j #706

Open ddanny opened 11 years ago

ddanny commented 11 years ago

I was a happy user of restlet 2.0.15 until yesterday, when they discontinued the 2.0.x versions from their Maven repository, so I had to upgrade to 2.1.1.

In the old version I was using SLF4J as a bridge to Log4J and I was able to get the restlet logs to my Log4J based logging system. This is no longer working with restlet 2.1.1. Any ideas what I would need to fix?

This is the way it was working:

public static void initLogging() {
  // install the SL4J bridge such as the org.restlet logging messages to
  // be appended to Log4J too, from the default java.util.logging based
  // implementation
  SLF4JBridgeHandler.install();
  ...
  if (log4j != null) {
      DOMConfigurator.configure(log4j);
  }
  log = Logger.getLogger(SomeClass.class);
}

Below is a list of the restlet and slf4j related dependencies I have in my pom.xml:

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.crypto</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.json</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.simple</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.ssl</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.slf4j</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>1.7.2</version>
    </dependency>

Once again, this was all working with restlet version 2.0.15, slf4j 1.6.4 and without any org.restlet.ext.slf4j dependency.

jlouvel commented 11 years ago

Thanks for the feed-back. The 2.0.5 artifacts were restored in Maven (hardware failure), however we won't maintain the 2.0 branch anymore.

Otherwise, 2.1 version should work the same regarding logging, but obviously a regression was introduced.

ddanny commented 11 years ago

I did some extra investigation and found out the it works fine with 2.1-M1 and doesn't work with 2.1-M2. This means that something has broken before releasing 2.1-M2.

ddanny commented 11 years ago

However, it works with the slf4j restlet extension and with this line System.setProperty("org.restlet.engine.loggerFacadeClass", "org.restlet.ext.slf4j.Slf4jLoggerFacade"); instead of SLF4JBridgeHandler.install();

jlouvel commented 11 years ago

Indeed, this is the way to go as indicated here: http://www.restlet.org/documentation/snapshot/jee/ext/org/restlet/ext/slf4j/Slf4jLoggerFacade.html

It would be worth being more clear in the Javadocs.

evandor commented 11 years ago

Not sure if I should open a new issue, maybe I am missing something... but I cannot get this approach (setting -Dorg.restlet.engine.loggerFacadeClass=org.restlet.ext.slf4j.Slf4jLoggerFacade) working in an OSGi-context (I am using felix), I am getting a ClassNotFoundException

Oct 9, 2013 10:36:17 AM org.restlet.engine.Engine <init>
WARNING: Unable to register the logger facade
java.lang.ClassNotFoundException: org.restlet.ext.slf4j.Slf4jLoggerFacade
    at org.restlet.engine.util.EngineClassLoader.findClass(EngineClassLoader.java:104)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.restlet.engine.Engine.<init>(Engine.java:447)
    at org.restlet.engine.Engine.register(Engine.java:343)
    at org.restlet.engine.internal.Activator.start(Activator.java:138)
    at org.apache.felix.framework.util.SecureAction$Actions.run(SecureAction.java:1605)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:636)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:1977)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:1895)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1191)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:295)
    at java.lang.Thread.run(Thread.java:662)

The org.restlet.ext.slf4j bundle is already resolved when this happens:

g! lb restlet
START LEVEL 6
   ID|State      |Level|Name
   21|Active     |    5|org.restlet (2.1.0.v20130116-1240)
   22|Active     |    5|org.restlet.ext.xml (2.1.0.v20130116-1240)
   23|Active     |    5|org.restlet.ext.jackson (2.1.0.v20130116-1240)
   24|Active     |    5|org.restlet.ext.xstream (2.1.0.v20130116-1240)
   25|Active     |    5|org.restlet.ext.json (2.1.0.v20130116-1240)
   26|Active     |    5|org.restlet.ext.crypto (2.1.0.v20130116-1240)
   27|Active     |    5|org.restlet.ext.servlet (2.1.0.v20130116-1240)
   69|Active     |    3|org.restlet.ext.slf4j (2.1.0.v20130116-1240)

Tweaking my local org.restlet bundle's manifest like this

Import-Package: javax.xml.parsers;resolution:=optional,org.osgi.framewor
 k;resolution:=optional,org.w3c.dom;resolution:=optional,org.w3c.dom.ls;
 resolution:=optional,org.xml.sax;resolution:=optional,org.xml.sax.helpe
 rs;resolution:=optional,org.restlet.ext.slf4j;resolution:=optional

made the problem go away (but I am not sure if this is the recommended approach for the restlet framework).

Restlet 2.1.4's Manifest looks the same, so i didn't try upgrading (yet).

Any hint welcome... (and, btw, thanks for this awesome framework ;))