Andy Dufresne
Andy Dufresne

Reputation: 6180

How to enable jersey trace logging in glassfish

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?

Upvotes: 1

Views: 6169

Answers (1)

unwichtich
unwichtich

Reputation: 13857

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:

Upvotes: 2

Related Questions