java - Implementing custom logging for Dropwizard resources -


i have dropwizard application multiple resources. need implement custom logging should same resources , should contain things url, response time, response size , data extracted request header. generally, able specify want there , in format. don't want have add code of resources (because there 50+ of them).

i did searching , found few interesting articles don't know how implement - few things tried didn't work:

i considered using aspect oriented programming, resources return response, aspectj requires spring don't want bring project reason.

i have found instrumentedhandler in dropwizard's metrics, this article looks need, cannot find example how plug application.

do have suggestions how implement logging?

this has done trick me:

import com.sun.jersey.api.container.filter.loggingfilter; import com.sun.jersey.spi.container.containerrequest; import com.sun.jersey.spi.container.containerrequestfilter; import com.sun.jersey.spi.container.containerresponse;  public class logfilter extends loggingfilter implements containerrequestfilter {      private final threadlocal<long> starttime = new threadlocal<>();      @override     public containerrequest filter(containerrequest containerrequest) {         // gets called when request comes in         starttime.set(system.currenttimemillis());         return containerrequest;     }      @override     public containerresponse filter(containerrequest request, containerresponse response) {         // gets called when response returned         // logging here         return response;     }  } 

and in application's run method:

@override public void run(myconfiguration configuration, environment environment) {     logfilter logfilter = new logfilter();     environment.jersey().getresourceconfig().getcontainerrequestfilters().add(logfilter);     environment.jersey().getresourceconfig().getcontainerresponsefilters().add(logfilter); } 

Comments