How do I get HTTP request logs on grizzly server console?

Viewed 463

Following the tutorial on Jersey's user guide, I created a small jax-rs based REST web application. This is the main class that starts the server.

public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";

/**
 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
 *
 * @return Grizzly HTTP server.
 */
public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in com.example package
    final ResourceConfig rc = new ResourceConfig().packages("com.example");

    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

/**
 * Main method.
 *
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    final HttpServer server = startServer();
    System.out.println(String.format("Jersey app started with WADL available at "
            + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
    System.in.read();
    server.shutdownNow();
   }
}

Resource method handler for GET request:

@Path("myresource")
public class MyResource {

/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
    return "Got it!";
   }
}

when I run the server, I get this on the console:

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" ... 
 Sep 10, 2018 11:51:55 AM org.glassfish.grizzly.http.server.NetworkListener 
 start
 INFO: Started listener bound to [localhost:8080]
 Jersey app started with WADL available at 
 http://localhost:8080/myapp/application.wadl
 Hit enter to stop it...
 Sep 10, 2018 11:51:55 AM org.glassfish.grizzly.http.server.HttpServer start
 INFO: [HttpServer] Started.

When I send a GET request via Postman to http://localhost:8080/myapp/myresource, it gives me a status 200 OK with "Got it!" as a response body, but nothing gets logged in the server console.

It might be a very basic question, but how can I implement it so that every time I send an HTTP request to the grizzly server, the requests and responses get logged on the console? I'd really appreciate any sort of help.

0 Answers
Related