jetty - How do I get Jersey 2.2 (JAX-RS) to generate log output, including Json request bodies -
i'm running jersey 2.2 servlet inside jetty 9.0.4 in order serve rest requests.
mostly , requests served, have never seen any log jersey classes. , can't find doco indicating chickens need sacrifice make happen jersey 2.2
so first question - need jersey generate log.
when request run awry (eg because json request body can't parsed) jersey throw containerexception message "can not deserialize instance of java.util.arraylist out of start_object token" etc. @ point really lovely have logged incoming request body can inspect json. again can't find in current doco outlining such beast although i'm sure there one. , in case until solve question 1 above it's moot.
so 2nd question how log incoming request body (without disrupting request).
the jersey servlet config in web.xml looks like:
<servlet > <servlet-name>jersey servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>au.com.xandar.wirelesstiming.recorder.web.rest.jerseyapplication</param-value> </init-param> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.filter.loggingfilter</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
my jerseyapplication is:
public final class jerseyapplication extends resourceconfig { public jerseyapplication() { super( //jacksonfeature.class // switching on jackson // (my) jerseyloggingfilter.class // log requests using jersey containerrequestfilter myapplicationeventlistener.class // log requests using jersey requesteventlistener ); packages("au.com.xandar.wirelesstiming.recorder"); // enable loggingfilter & output entity. // nb not generate log. registerinstances(new loggingfilter(logger.getlogger(jerseyapplication.class.getname()), true)); } }
jersey doesn't log itself. 2.x we're working on development mode include tracing support (see tracing in jersey), default logging of incoming requests / outgoing responses (incl. entity bodies , tracing messages). feature should out soon.
regarding second question - can register loggingfilter able log incoming requests / outgoing responses (method, headers, ..) entity bodies (when configured). can configure via (3rd option illustrates how turn-on entity logging):
web.xml
(add jax-rs servlet definition):
<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.filter.loggingfilter</param-value> </init-param>
application
extension:
public class myapplication extends application { @override public set<class<?>> getclasses() { return new hashset<class<?>>() {{ // add resources. add(helloworldresource.class); // add loggingfilter. add(loggingfilter.class); }}; } }
resourceconfig
instance (demonstrating outputting entity here):
public class myapplication extends resourceconfig { public myapplication() { // resources - add package name here enable package scanning. packages(...); // enable loggingfilter & output entity. registerinstances(new loggingfilter(logger.getlogger(myapplication.class.getname()), true)); } }
Comments
Post a Comment