原文: https://howtodoinjava.com/jersey/jersey-custom-logging-request-and-response-entities-using-filter/

默认情况下,Jersey 使用 JUL 进行日志记录 - 不会在日志中打印请求/响应实体主体。 要打印实体内容,必须创建自己的LoggingFiler,并将其注册为默认值org.glassfish.jersey.filter.LoggingFilter。 在此示例中,我正在创建一个这样的基本CustomLoggingFilter,它扩展了org.glassfish.jersey.filter.LoggingFilter并实现了ContainerRequestFilterContainerResponseFilter接口,以便它可以拦截正在进行的请求和响应。

  1. Table of Contents
  2. Create CustomLoggingFilter
  3. Register CustomLoggingFilter
  4. Log statements with default LoggingFilter
  5. Log statements with CustomLoggingFilter

创建CustomLoggingFilter

让我们直接转到此 Jersey 演示的自定义日志记录过滤器类。

  1. package com.howtodoinjava.jersey.provider;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import javax.ws.rs.container.ContainerRequestContext;
  7. import javax.ws.rs.container.ContainerRequestFilter;
  8. import javax.ws.rs.container.ContainerResponseContext;
  9. import javax.ws.rs.container.ContainerResponseFilter;
  10. import org.glassfish.jersey.filter.LoggingFilter;
  11. import org.glassfish.jersey.message.internal.ReaderWriter;
  12. public class CustomLoggingFilter extends LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter
  13. {
  14. @Override
  15. public void filter(ContainerRequestContext requestContext) throws IOException
  16. {
  17. StringBuilder sb = new StringBuilder();
  18. sb.append("User: ").append(requestContext.getSecurityContext().getUserPrincipal() == null ? "unknown"
  19. : requestContext.getSecurityContext().getUserPrincipal());
  20. sb.append(" - Path: ").append(requestContext.getUriInfo().getPath());
  21. sb.append(" - Header: ").append(requestContext.getHeaders());
  22. sb.append(" - Entity: ").append(getEntityBody(requestContext));
  23. System.out.println("HTTP REQUEST : " + sb.toString());
  24. }
  25. private String getEntityBody(ContainerRequestContext requestContext)
  26. {
  27. ByteArrayOutputStream out = new ByteArrayOutputStream();
  28. InputStream in = requestContext.getEntityStream();
  29. final StringBuilder b = new StringBuilder();
  30. try
  31. {
  32. ReaderWriter.writeTo(in, out);
  33. byte[] requestEntity = out.toByteArray();
  34. if (requestEntity.length == 0)
  35. {
  36. b.append("").append("\n");
  37. }
  38. else
  39. {
  40. b.append(new String(requestEntity)).append("\n");
  41. }
  42. requestContext.setEntityStream( new ByteArrayInputStream(requestEntity) );
  43. } catch (IOException ex) {
  44. //Handle logging error
  45. }
  46. return b.toString();
  47. }
  48. @Override
  49. public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException
  50. {
  51. StringBuilder sb = new StringBuilder();
  52. sb.append("Header: ").append(responseContext.getHeaders());
  53. sb.append(" - Entity: ").append(responseContext.getEntity());
  54. System.out.println("HTTP RESPONSE : " + sb.toString());
  55. }
  56. }

注册CustomLoggingFilter

要注册此CustomLoggingFilter,请以这种方式注册。

  1. package com.howtodoinjava.jersey;
  2. import org.glassfish.jersey.jackson.JacksonFeature;
  3. import org.glassfish.jersey.server.ResourceConfig;
  4. import com.howtodoinjava.jersey.provider.CustomLoggingFilter;
  5. public class CustomApplication extends ResourceConfig
  6. {
  7. public CustomApplication()
  8. {
  9. packages("com.howtodoinjava.jersey");
  10. register(JacksonFeature.class);
  11. register(CustomLoggingFilter.class);
  12. }
  13. }

使用默认LoggingFilter的日志语句

现在,如果您尝试使用不带CustomLoggingFilter的任何现有 REST API,日志将以这种方式显示。

请求

Jersey 使用过滤器记录请求和响应实体 - 图1

日志

  1. Sep 30, 2015 6:18:41 PM org.glassfish.jersey.filter.LoggingFilter log
  2. INFO: 1 * Server has received a request on thread http-bio-8080-exec-4
  3. 1 > POST http://localhost:8080/JerseyDemos/rest/employees
  4. 1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. 1 > accept-encoding: gzip, deflate
  6. 1 > accept-language: null
  7. 1 > cache-control: no-cache
  8. 1 > connection: keep-alive
  9. 1 > content-length: 35
  10. 1 > content-type: application/json; charset=UTF-8
  11. 1 > host: localhost:8080
  12. 1 > pragma: no-cache
  13. 1 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
  14. Sep 30, 2015 6:18:41 PM org.glassfish.jersey.filter.LoggingFilter log
  15. INFO: 1 * Server responded with a response on thread http-bio-8080-exec-4
  16. 1 < 200
  17. 1 < Content-Type: application/json

使用CustomLoggingFilter的日志语句

添加CustomLoggingFilter后,您将获得更好的日志,如下所示。

  1. HTTP REQUEST : User: unknown - Path: employees - Header: {host=[localhost:8080], user-agent=[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0], accept=, accept-language=[null], accept-encoding=[gzip, deflate], content-type=[application/json; charset=UTF-8], content-length=[35], connection=[keep-alive], pragma=[no-cache], cache-control=[no-cache]} - Entity: {"id":2,"name":"Alex Kolenchiskey"}
  2. HTTP RESPONSE : Header: {Content-Type=[application/json]} - Entity: Employee [id=2, name=Alex Kolenchiskey]

请根据需要随时从日志语句中添加/删除信息。 您可以在这些日志中添加许多其他有用的信息。

祝您学习愉快!