On 02.08.2013, at 11:25, Piers Powlesland <piers_at_aptusinteractive.com> wrote:
> Hi I'm trying to log requests as they come into the web application I am writing. I cannot see how to do this I have managed to log both requests and responses by calling "register(new LoggingFilter())" in my resource configuration class, but it is not clear to me how I can apply a filter to requests only?
Did you write LoggingFilter yourself?
You should implement a filter that only extends ContainerRequestFilter, not also ContainerResponseFilter.
Jan
E.g:
package xxxx;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
public class ContainerLoggingFilter implements ContainerRequestFilter {
private static Logger log = Logger.getLogger(ContainerLoggingFilter.class.getName());
private String name;
public ContainerLoggingFilter(String name) {
this.name = name;
}
@Override
public void filter(ContainerRequestContext req) throws IOException {
log.log(Level.INFO,
name + "REQ START ------------------------------");
log.log(Level.INFO, name + "> " + req.getMethod() + " ");
MultivaluedMap<String, String> headers = req.getHeaders();
for (String key : headers.keySet()) {
List<String> values = headers.get(key);
log.log(Level.INFO, name + "> " + key + ": " + values);
}
log.log(Level.INFO,
name + "REQ END ------------------------------");
}
}
>
> Anyone have an idea?
>
> Thanks
>
> Piers