How do I get Jersey 2.2 (JAX-RS) to generate log output, including Json request bodies

32,789

Jersey doesn't log much by itself. For 2.x we're working on a development mode which would include tracing support (see Tracing in Jersey), default logging of incoming requests / outgoing responses (incl. entity bodies and tracing messages). This feature should be out soon.

Regarding your second question - you can register LoggingFilter which is able to log incoming requests / outgoing responses (method, headers, ..) as well as entity bodies (when configured). You can configure it via (3rd option illustrates how to turn-on entity logging):

web.xml (add it to the JAX-RS servlet definition):

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>

Application extension:

public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>() {{
            // Add your resources.
            add(HelloWorldResource.class);

            // Add LoggingFilter.
            add(LoggingFilter.class);
        }};
    }
}

ResourceConfig instance (demonstrating also outputting the entity here):

public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Resources - add your package name here to enable package scanning.
        packages(...);

        // Enable LoggingFilter & output entity.     
        registerInstances(new LoggingFilter(Logger.getLogger(MyApplication.class.getName()), true));
    }
}
Share:
32,789
William
Author by

William

I'm been coding commercially since 1992 and working almost solely with Java since 1999. Started Android development in 2009 and have developed several commercial apps, the biggest hit being Lexathon with 600k downloads. Was a Google Developer expert from 2012 - 2017 until my startup took off. Am now founder and CEO of Wylas Timing which has developed an automated timing system for swimming and athletics that is 100% wireless.

Updated on July 31, 2022

Comments

  • William
    William almost 2 years

    I'm running a Jersey 2.2 Servlet inside Jetty 9.0.4 in order to serve REST requests.

    Mostly everything is good and requests get served, but I have never seen ANY log from Jersey classes. And I can't find any doco indicating what chickens I need to sacrifice to make that happen with Jersey 2.2

    So my first question is - what do I need to do to get Jersey to generate some log.

    When a request does run awry (eg because the Json request body can't be parsed) Jersey will throw a ContainerException with message like "Can not deserialize instance of java.util.ArrayList out of START_OBJECT token" etc. At that point it would be really lovely to have logged the incoming request body so I can inspect the Json. Again I can't find anything in the current doco outlining such a beast although I'm sure there is one. And in any case until I solve question 1 up above it's moot.

    So my 2nd question is how do I log the incoming request body (without disrupting the request).

    The Jersey Servlet config in web.xml looks like:

    <servlet >
        <servlet-name>Jersey Servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>au.com.xandar.wirelesstiming.recorder.web.rest.JerseyApplication</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    My JerseyApplication is:

    public final class JerseyApplication extends ResourceConfig {
    
        public JerseyApplication() {
            super(
                //JacksonFeature.class   // Switching on Jackson
                // (My) JerseyLoggingFilter.class       // Log requests using Jersey ContainerRequestFilter 
                MyApplicationEventListener.class        // Log Requests using Jersey RequestEventListener
            );
            packages("au.com.xandar.wirelesstiming.recorder");
    
            // Enable LoggingFilter & output entity.
            // NB This does NOT generate any log.
            registerInstances(new LoggingFilter(Logger.getLogger(JerseyApplication.class.getName()), true));
        }
    }
    
  • William
    William almost 11 years
    Michal, I tried adding the web.xml version but I still no logging of the Requests. I've updated my question to include the web.xml snippet
  • William
    William almost 11 years
    I'm not getting any log for option 3 either.
  • Michal Gajdos
    Michal Gajdos almost 11 years
    You should see some logs done by Jersey (at INFO level) in <jetty_home>/logs/*.stderrout.log, at least something like Aug 16, 2013 12:15:24 PM org.glassfish.jersey.server.ApplicationHandler initialize INFO: Initiating Jersey application, version Jersey: 2.3-SNAPSHOT 2013-08-14 18:36:24... Isn't this the case?
  • William
    William almost 11 years
    I'm running Jetty via the maven-jetty-plugin, so no jetty_home. Jetty log normally just goes to System.out. I'm not seeing any log about Jersey app initialisation or any other Jersey log (except below). But now that I have the JerseyLogger configured in both web.xml and in MyApplication I'm getting: Aug 16, 2013 8:28:41 PM org.glassfish.jersey.internal.Errors logErrors WARNING: The following warnings have been detected: WARNING: Cannot new create registration for component type class org.glassfish.jersey.filter.LoggingFilter: Existing previous registration found for the type.
  • Michal Gajdos
    Michal Gajdos almost 11 years
    Since you want to see request entity bodies remove the web.xml init-parram adding the logging filter and leave only the Application one. Can you see other INFO logs (i.e. initialization of Jetty server: 2013-08-16 12:49:28.983:INFO:oejs.Server:main: jetty-9.0.4.v20130625)?
  • William
    William almost 11 years
    OK, removed the web.xml config, LoggingFilter only configured in (My)JerseyApplication. Now there is no Jersey log at all. But i see all of my log and all Jetty log like 12013-08-16 21:16:34.830:INFO:oejs.Server:main: jetty-9.0.4.v20130625 `
  • Michal Gajdos
    Michal Gajdos almost 11 years
    Can you show us your JerseyApplication? Do you use some other logging mechanisms in your app?
  • William
    William almost 11 years
    JerseyApplication added to question. I log using the Jetty logger API configured to write to Std err. I have switched on DEBUG for my classes and Jersey and INFO for Jetty.
  • William
    William over 10 years
    I feel really stupid here, but OOTB config for Jetty only logs Jetty logs at INFO and I set my own classes to log at DEBUG etc. Configuring Jetty so that all classes log at DEBUG shows the Jersey log. Mea culpa.
  • William
    William over 10 years
    Michal, is there some way to switch off the Jersey ApplicationHandler log on startup?
  • Michal Gajdos
    Michal Gajdos over 10 years
    Not really. If you consider this to be a security issue, please file a improvement in our JIRA (java.net/jira/browse/JERSEY) and we'll come up with a property that would turn it off.
  • William
    William over 10 years
    Not so much a security issue as a UX issue. Created java.net/jira/browse/JERSEY-2365
  • Ed Pike
    Ed Pike over 8 years
    I'm not sure this is helpful, but I'm running Jersey 2 in tomcat 7 as a windows service. I enabled the logging filter in web.xml and unfortunately they come out as INFO entries in the stderr log that the windows wrapper creates, not stdout, like you'd expect. Since this is consistent with above comments, it must be a feature, not a bug.
  • Frederick Nord
    Frederick Nord almost 8 years
    "For 2.x we're working on a development mode which would include tracing support" any news here? I'm a Jersey newbie and I think I really need such a simple tracer or a some very verbose logging.
  • gaurav5430
    gaurav5430 over 7 years
    Not getting any logs when configured in web.xml. it works when i register it using ResourceConfig subclass
  • Robert Mikes
    Robert Mikes almost 7 years
    LoggingFilter is now deprecated, we should use LoggingFeature instead. But how to do it in web.xml with LoggingFeature?