Logging question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Logging question

kevinb
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();
    }
}

Reply | Threaded
Open this post in threaded view
|

Re: Logging question

Ryan Lubke-2
On 12/14/11 2: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.
We don't have much in the way of logging in this area at the moment.

Are you trying to debug an issue and need to see the http traffic?  If
so, I'd recommend ngrep or wireshark.  Or are you looking for something
more traditional like an access log?

Thanks,
-rl

>
> 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();
>     }
> }
>

Reply | Threaded
Open this post in threaded view
|

Re: Logging question

oleksiys
Administrator
In reply to this post by kevinb
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();
    }
}


Reply | Threaded
Open this post in threaded view
|

Re: Logging question

kevinb
OK, this worked, code below for anyone who is interested.



package com.frk.jerseyrestgrizzly;

import org.glassfish.grizzly.filterchain.*;

import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.HttpContent;
import org.glassfish.grizzly.http.HttpHeader;
import org.glassfish.grizzly.http.HttpProbe;
import org.glassfish.grizzly.http.ContentEncoding;
import org.glassfish.grizzly.http.TransferEncoding;
import org.glassfish.grizzly.http.HttpPacket;
import org.glassfish.grizzly.http.HttpCodecFilter;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Connection;


import org.glassfish.grizzly.http.jmx.*;
import org.glassfish.grizzly.http.server.StaticHttpHandler;

/**
 * Hello world!
 *
 */
public class App {

    /**
     * @param args the command line arguments
     */
    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(8080).build();
    }
    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.jerseyrestgrizzly");

        System.out.println("Starting grizzly...");

        return GrizzlyWebContainerFactory.create(BASE_URI, initParams);

    }

    public static void main(String[] args) throws IOException {
        final FilterChain filterChain;

        HttpServer httpServer = startServer();

        httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("C:\\Projects\\JerseyRESTGrizzly\\test\\"), "/test");

        filterChain = httpServer.getListener("grizzly").getFilterChain();

        // Get HttpCodecFilter

        HttpCodecFilter codecFilter = (HttpCodecFilter) filterChain.get(filterChain.indexOfType(HttpCodecFilter.class));
        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) {
            }

            public void onErrorEvent(Connection cnctn, Throwable thrwbl) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });



        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();

    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Logging question

Ryan Lubke-2
Thanks for sharing.

Just off the cuff, it seems we could improve HttpProbe by providing a
default implementation that no-ops the methods so you could just
override what you're interested in - less clutter.

On 1/6/12 1:46 PM, kevinb wrote:

> OK, this worked, code below for anyone who is interested.
>
>
>
> package com.frk.jerseyrestgrizzly;
>
> import org.glassfish.grizzly.filterchain.*;
>
> import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
> import org.glassfish.grizzly.http.server.HttpServer;
> import org.glassfish.grizzly.http.HttpContent;
> import org.glassfish.grizzly.http.HttpHeader;
> import org.glassfish.grizzly.http.HttpProbe;
> import org.glassfish.grizzly.http.ContentEncoding;
> import org.glassfish.grizzly.http.TransferEncoding;
> import org.glassfish.grizzly.http.HttpPacket;
> import org.glassfish.grizzly.http.HttpCodecFilter;
>
> import javax.ws.rs.core.UriBuilder;
> import java.io.IOException;
> import java.net.URI;
> import java.util.HashMap;
> import java.util.Map;
> import org.glassfish.grizzly.Buffer;
> import org.glassfish.grizzly.Connection;
>
>
> import org.glassfish.grizzly.http.jmx.*;
> import org.glassfish.grizzly.http.server.StaticHttpHandler;
>
> /**
>   * Hello world!
>   *
>   */
> public class App {
>
>      /**
>       * @param args the command line arguments
>       */
>      private static URI getBaseURI() {
>          return UriBuilder.fromUri("http://localhost/").port(8080).build();
>      }
>      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.jerseyrestgrizzly");
>
>          System.out.println("Starting grizzly...");
>
>          return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
>
>      }
>
>      public static void main(String[] args) throws IOException {
>          final FilterChain filterChain;
>
>          HttpServer httpServer = startServer();
>
>          httpServer.getServerConfiguration().addHttpHandler(new
> StaticHttpHandler("C:\\Projects\\JerseyRESTGrizzly\\test\\"), "/test");
>
>          filterChain = httpServer.getListener("grizzly").getFilterChain();
>
>          // Get HttpCodecFilter
>
>          HttpCodecFilter codecFilter = (HttpCodecFilter)
> filterChain.get(filterChain.indexOfType(HttpCodecFilter.class));
>          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) {
>              }
>
>              public void onErrorEvent(Connection cnctn, Throwable thrwbl) {
>                  throw new UnsupportedOperationException("Not supported
> yet.");
>              }
>          });
>
>
>
>          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();
>
>      }
> }
>
>
> --
> View this message in context: http://grizzly.1045725.n5.nabble.com/Logging-question-tp5076118p5126754.html
> Sent from the Grizzly - Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Logging question

Ryan Lubke-2
On 1/6/12 2:03 PM, Ryan Lubke wrote:
> Thanks for sharing.
>
> Just off the cuff, it seems we could improve HttpProbe by providing a
> default implementation that no-ops the methods so you could just
> override what you're interested in - less clutter.
Alexey logged http://java.net/jira/browse/GRIZZLY-1171 this morning to
address the comment above.

We've applied the changes to resolve it just a few minutes ago and will
be available in 2.1.9 and 2.2.1.

So now you can do:

HttpServerProbe probe = new HttpServerProbe.Adapter() {

    // override only the methods you're interested in

};

This applies to all Probe types.

>
> On 1/6/12 1:46 PM, kevinb wrote:
>> OK, this worked, code below for anyone who is interested.
>>
>>
>>
>> package com.frk.jerseyrestgrizzly;
>>
>> import org.glassfish.grizzly.filterchain.*;
>>
>> import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
>> import org.glassfish.grizzly.http.server.HttpServer;
>> import org.glassfish.grizzly.http.HttpContent;
>> import org.glassfish.grizzly.http.HttpHeader;
>> import org.glassfish.grizzly.http.HttpProbe;
>> import org.glassfish.grizzly.http.ContentEncoding;
>> import org.glassfish.grizzly.http.TransferEncoding;
>> import org.glassfish.grizzly.http.HttpPacket;
>> import org.glassfish.grizzly.http.HttpCodecFilter;
>>
>> import javax.ws.rs.core.UriBuilder;
>> import java.io.IOException;
>> import java.net.URI;
>> import java.util.HashMap;
>> import java.util.Map;
>> import org.glassfish.grizzly.Buffer;
>> import org.glassfish.grizzly.Connection;
>>
>>
>> import org.glassfish.grizzly.http.jmx.*;
>> import org.glassfish.grizzly.http.server.StaticHttpHandler;
>>
>> /**
>>   * Hello world!
>>   *
>>   */
>> public class App {
>>
>>      /**
>>       * @param args the command line arguments
>>       */
>>      private static URI getBaseURI() {
>>          return
>> UriBuilder.fromUri("http://localhost/").port(8080).build();
>>      }
>>      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.jerseyrestgrizzly");
>>
>>          System.out.println("Starting grizzly...");
>>
>>          return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
>>
>>      }
>>
>>      public static void main(String[] args) throws IOException {
>>          final FilterChain filterChain;
>>
>>          HttpServer httpServer = startServer();
>>
>>          httpServer.getServerConfiguration().addHttpHandler(new
>> StaticHttpHandler("C:\\Projects\\JerseyRESTGrizzly\\test\\"), "/test");
>>
>>          filterChain =
>> httpServer.getListener("grizzly").getFilterChain();
>>
>>          // Get HttpCodecFilter
>>
>>          HttpCodecFilter codecFilter = (HttpCodecFilter)
>> filterChain.get(filterChain.indexOfType(HttpCodecFilter.class));
>>          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) {
>>              }
>>
>>              public void onErrorEvent(Connection cnctn, Throwable
>> thrwbl) {
>>                  throw new UnsupportedOperationException("Not supported
>> yet.");
>>              }
>>          });
>>
>>
>>
>>          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();
>>
>>      }
>> }
>>
>>
>> --
>> View this message in context:
>> http://grizzly.1045725.n5.nabble.com/Logging-question-tp5076118p5126754.html
>> Sent from the Grizzly - Users mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Logging question

kevinb
Hi Ryan-

Thanks for the update, I will definitely test this out when I get my POC finished.

Kevin