users@grizzly.java.net

Re: Logging question

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Thu, 15 Dec 2011 11:00:37 +0100

Hi Kevin,

as Ryan mentioned there is no property to enable/disable HTTP message
logging, though there is a way to implement logging yourself (we'll
appreciate if you can donate it back then ;) )
There is a way to register HttpCodec monitoring probe, unfortunately
Jersey returns started HttpServer, so the way to register probe is a bit
more complex... anyway here is what you can do:

/ // Get underlying Grizzly HttpServer's FilterChain
             final FilterChain filterChain =
httpServer.getListener("grizzly").getFilterChain();

             // Get HttpCodecFilter
             HttpCodecFilter codecFilter =
                     (HttpCodecFilter) filterChain.get(
                     filterChain.indexOfType(HttpCodecFilter.class));

             // Register HttpProbe
         codecFilter.getMonitoringConfig().addProbes(new HttpProbe() {

                 public void onDataReceivedEvent(Connection connection,
Buffer buffer) {
                     
System.out.println(buffer.toStringContent()); // Log incoming
traffic
                 }

                 public void onDataSentEvent(Connection connection,
Buffer buffer) {
                     
System.out.println(buffer.toStringContent()); // //// Log
outgoing traffic/
/ }

                 public void onHeaderParseEvent(Connection connection,
HttpHeader header, int size) {
                 }

                 public void onHeaderSerializeEvent(Connection
connection, HttpHeader header, Buffer buffer) {
                 }

                 public void onContentChunkParseEvent(Connection
connection, HttpContent content) {
                 }

                 public void onContentChunkSerializeEvent(Connection
connection, HttpContent content) {
                 }

                 public void onContentEncodingParseEvent(Connection
connection, HttpHeader header, Buffer buffer, ContentEncoding
contentEncoding) {
                 }

                 public void onContentEncodingSerializeEvent(Connection
connection, HttpHeader header, Buffer buffer, ContentEncoding
contentEncoding) {
                 }

                 public void onTransferEncodingParseEvent(Connection
connection, HttpHeader header, Buffer buffer, TransferEncoding
transferEncoding) {
                 }

                 public void onTransferEncodingSerializeEvent(Connection
connection, HttpHeader header, Buffer buffer, TransferEncoding
transferEncoding) {
                 }

                 public void onErrorEvent(Connection connection,
HttpPacket httpPacket, Throwable error) {
                 }
             });
/

On 12/14/2011 11:17 PM, Kevin wrote:
> Hi All-
>
> How do I log incoming request to the Grizzly container? I've searched
> hi and low for this. Any direction would be appreciated.
>
> Thanks,
> Kevin
>
> My code
>
> public class GrizzlyJersey {
>
> /**
> * @param args the command line arguments
> */
> private static URI getBaseURI() {
> return UriBuilder.fromUri("http://localhost/").port(8080).build();
> }
>
> private final static Logger LOGGER =
> Logger.getLogger(GrizzlyJersey.class .getName());
>
> public static final URI BASE_URI = getBaseURI();
>
> protected static HttpServer startServer() throws IOException {
> final Map<String, String> initParams = new HashMap<String,
> String>();
>
> initParams.put("com.sun.jersey.config.property.packages",
> "com.frk.grizzlyjersey");
>
> System.out.println("Starting grizzly...");
> return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
> }
>
> public static void main(String[] args) throws IOException {
>
>
>
> HttpServer httpServer = startServer();
>
> LOGGER.setLevel(Level.ALL);
>
> System.out.println(String.format("Jersey app started with WADL
> available at "
> + "%sapplication.wadl\nTry out %shelloworld\nHit enter
> to stop it...",
> BASE_URI, BASE_URI));
> System.in.read();
> httpServer.stop();
> }
> }
>