Chapter 21. Logging
Prev |  | Next

Links: Table of Contents | Single HTML

# Chapter 21. Logging

## 21.1. Logging traffic

### 21.1.1. Introduction

Jersey Logging supports the logging request and response via internal client and server filters, which are configured and registered by LoggingFeature 's properties. `LoggingFeature` has been introduced in Jersey 2.23 version and deprecates an older `LoggingFilter`.

`LoggingFeature` might be discovered by auto-discoverable mechanism or initialized by registering on client or server components. Client or server logging filter is initialized depending on which context is `LoggingFeature` registered with.

### 21.1.2. Configuration and registering

#### 21.1.2.1. Configuration options

Configurable options

- `Logger name`

  Defines a logger used to log request and response messages.

  Default value is LoggingFeature.DEFAULT_LOGGER_NAME.

- `Logger level`

  Defines level that will be used to log messages by logging filters. Messages will be logged only if the effective level of the logger allows it.

  Default value is LoggingFeature.DEFAULT_LOGGER_LEVEL.

- `Verbosity`

  Verbosity determines how detailed message will be logged. See LoggingFeature.Verbosity javadoc.

  - The lowest verbosity LoggingFeature.Verbosity.HEADERS_ONLY will log only request/response headers.

  - The medium verbosity (LoggingFeature.Verbosity.PAYLOAD_TEXT) will log request/response headers, as well as an entity if considered a readable text. The entity is considered a readable text, if MediaType is `text/*` or is one of

    - `application/atom+xml`

    - `application/json`

    - `application/svg+xml`

    - `application/x-www-form-urlencoded`

    - `application/xhtml+xml`

    - `application/xml`

  - The highest verbosity LoggingFeature.Verbosity.PAYLOAD_ANY will log all types of an entity (besides the request/response headers.

  Note that the entity is logged up to the specified maximum number of bytes (see LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE).

  Default value is LoggingFeature.DEFAULT_VERBOSITY.

- `Maximum entity size`

  Maximum number of entity bytes to be logged (and buffered) - if the entity is larger, logging filter will print (and buffer in memory) only the specified number of bytes and print "...more..." string at the end. Negative values are interpreted as zero.

  Default value LoggingFeature.DEFAULT_MAX_ENTITY_SIZE.

#### 21.1.2.2. Configuration properties

The feature is enabled on when auto-discoverable mechanism is not disabled and at least one of the feature's property is set. For enabling client or server logging filter one of the common properties or `_CLIENT` suffixed properties, or `_SERVER` properties respectively.

An example of initializing server-side logging with the highest verbosity.

Example 21.1. Logging on the client side

        ClientConfig clientConfig = new ClientConfig();
        clientConfig.property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY);
        Client client = ClientBuilder.newClient(clientConfig);

The `LoggingFeature` might be registered explicitly on ResourceConfig for server-side logging or on Client for client-side logging.

Example 21.2. Register `LoggingFeature` via constructor

            ResourceConfig config = new ResourceConfig(HelloWorldResource.class);
            config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));

Following examples demonstrate registering `LoggingFeature` on server-side with default values and values defined by one of the public constructors (see LoggingFeature).

Example 21.3. Register `LoggingFeature` class

            ResourceConfig config = new ResourceConfig(HelloWorldResource.class);
            config.register(LoggingFeature.class);

An example of server-side logging with entity `Hello World!`

      1 May 09, 2016 2:55:33 PM org.glassfish.jersey.logging.LoggingInterceptor log
      2 INFO: 1 * Server has received a request on thread grizzly-http-server-0
      3 1 > GET http://localhost:9998/helloworld
      4 1 > accept: text/plain
      5 1 > accept-encoding: gzip,deflate
      6 1 > connection: Keep-Alive
      7 1 > host: localhost:9998
      8 1 > user-agent: Jersey/3.0-SNAPSHOT (Apache HttpClient 4.5)
      9
     10 May 09, 2016 2:55:33 PM org.glassfish.jersey.logging.LoggingInterceptor log
     11 INFO: 1 * Server responded with a response on thread grizzly-http-server-0
     12 1 < 200
     13 1 < Content-Type: text/plain
     14 Hello World!

Prev |  | Next
Chapter 20. MVC Templates | Home | Chapter 22. Monitoring and Diagnostics