1

I've been writing RESTful Web service. The technologies that I use: Glassfish 4, JDK 8 & Jersey (part of glassfish).

To troubleshoot an issue, I want to enable trace logging done by jersey classes. For e.g. below is the logging code in the _getMessageBodyWriter() method of MessageBodyFactory

 final TracingLogger tracingLogger = TracingLogger.getInstance(propertiesDelegate);
    MessageBodyWriter<T> selected = null;
    final Iterator<MbwModel> iterator = writers.iterator();
    while (iterator.hasNext()) {
        final MbwModel model = iterator.next();
        if (model.isWriteable(c, t, as, mediaType)) {
            selected = (MessageBodyWriter<T>) model.provider;
            tracingLogger.log(MsgTraceEvent.MBW_SELECTED, selected);
            break;
        }
        tracingLogger.log(MsgTraceEvent.MBW_NOT_WRITEABLE, model.provider);
    }

    if (tracingLogger.isLogEnabled(MsgTraceEvent.MBW_SKIPPED)) {
        while (iterator.hasNext()) {
            final MbwModel model = iterator.next();
            tracingLogger.log(MsgTraceEvent.MBW_SKIPPED, model.provider);
        }
    }

How do I enable this logging through logging.properties file?

Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113
  • Look in the admin console, there is a section for administering and configuring logging services for all GlassFish's components. Also have a look at the documentation: [About Logging](https://docs.oracle.com/cd/E18930_01/html/821-2416/abluk.html#scrolltoc) – perissf Jul 13 '17 at 08:58
  • I assume you are referring the Logger Settings link on the instance configuration. Isn't this about jersey logging though? There are options of adding loggers on the "Log Levels" tab. What loggers should I add to get the above log messages seen in the server.log? – Andy Dufresne Jul 14 '17 at 10:03

1 Answers1

2

Here is an example:

@ApplicationPath("rest")
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        super();
        register(LoggingFilter.class);
        // register your rest classes here
        property("jersey.config.server.tracing.type", "ALL");
        property("jersey.config.server.tracing.threshold", "VERBOSE");
    }
}

Set the following in your logging.properties:

org.glassfish.jersey.tracing.level=ALL

This will activate jersey trace logging via HTTP headers and server.log, you can use something like Firebug or the Chrome developer console to see the headers.

By setting jersey.config.server.tracing.type to ALL you enable trace logging for every request. You can also set it to ON_DEMAND, then you have to add a header named X-Jersey-Tracing-Accept (value doesn't matter) to your request for logging.

There is a limit for headers per request in Glassfish, if you reach this limit with the logging you can do this via asadmin:

set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.max-response-headers=1000

See also:

unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • I did follow the steps and tried to enable logging in a embedded glassfish app (for debugging purposes) but I don't see the trace logs. Please have a look at the logging.properties file - https://pastebin.com/2ECzuUxJ. I tried to put a debug point in the ApplicationConfig but it seems the constructor is not called. What could be wrong? – Andy Dufresne Jul 24 '17 at 08:45
  • Your logging.properties file looks ok. Can you see the tracing logs in the HTTP headers? Did you use the ApplicationConfig before or did you only added it based on my answer? – unwichtich Jul 24 '17 at 09:30
  • I didn't add the HTTP header since I set ALL for "jersey.config.server.tracing.type". About ApplicationConfig, yes I only added it for enabling logging based on your answer. There isn't any other functional need of it. – Andy Dufresne Jul 25 '17 at 10:03
  • No I mean can you see the HTTP headers in the response? I guess the ApplicationConfig may not get loaded because you have configured your application in a different way. How are you registering your REST classes? – unwichtich Jul 25 '17 at 10:24
  • I see the standard headers. Nothing specific. Please find them here - https://pastebin.com/p8jvxEFA. Here's how I register the REST classes - https://pastebin.com/9fun0wxY and here's the code for starting the embedded jetty server - https://pastebin.com/enuwbq80. Thanks for the discussion so far. Please share your valuable inputs. – Andy Dufresne Jul 26 '17 at 06:05
  • Hmm...I don't really understand why you are using Jetty in your test environment when all the text in the question is about Glassfish? That are 2 completly different things. In your case I guess it could help to add the `LoggingFilter` to `jersey.config.server.provider.classnames` and set `org.glassfish.jersey.tracing.level=ALL` in your jetty-logging.properties. – unwichtich Jul 26 '17 at 14:55