Dropwizard Jersey 将 request/response 记录为错误,但为什么呢?

Dropwizard Jersey logging request/response as ERROR, but why?

我添加了这一行

environment.jersey().register(new LoggingFeature(Logger
    .getLogger(LoggingFeature.class.getName()), Level.OFF, 
          LoggingFeature.Verbosity.PAYLOAD_TEXT, null));

在我的 run(Configuration configuration, Environment environment) 方法中查看 and this

如果 Level 设置为 OFF 并且我的所有 requests/response 消息都记录为错误,我只会在日志中获取内容,但为什么错误?

这是一个日志示例:

ERROR  [11:17:41.603] [dw-31 - GET /helloworld] o.g.j.l.LoggingFeature -  1 

* Server has received a request on thread dw-31 - GET /helloworld
1 > GET http://localhost:8080/helloworld
1 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
1 > Accept-Encoding: gzip, deflate, sdch, br
1 > Accept-Language: en-US,en;q=0.8
1 > Cache-Control: max-age=0
1 > Connection: keep-alive
1 > Cookie: openid_provider=openid; _ga=GA1.1.1009619719.1483436711
1 > Host: localhost:8080
1 > Upgrade-Insecure-Requests: 1
1 > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36

ERROR  [11:17:41.615] [dw-31 - GET /helloworld] o.g.j.l.LoggingFeature -  1 * Server responded with a response on thread dw-31 - GET /helloworld
1 < 200

我的资源class:

@Path("/helloworld")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {

    public HelloWorldResource() { }

    @GET
    public Response helloWorld(){
        System.out.println("Hello World");

        return Response.ok().build();

    }
}

我的主要应用class:

public class ApiApplication extends Application<ApiConfiguration>{

public static void main(String[] args) throws Exception{
    new ApiApplication().run(args);
}

@Override
public void initialize(Bootstrap<ApiConfiguration> bootstrap) {
    // nothing to do yet
}

@Override
public void run(ApiConfiguration apiConfiguration, Environment environment) throws Exception {

    final TemplateHealthCheck healthCheck =
            new TemplateHealthCheck(apiConfiguration.getTemplate());
    environment.healthChecks().register("template", healthCheck);

    final HelloWorldResource helloWorldResource = new HelloWorldResource();
    final JerseyEnvironment jerseyEnvironment = environment.jersey();
    jerseyEnvironment.register(helloWorldResource);

    jerseyEnvironment.register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.OFF, LoggingFeature.Verbosity.PAYLOAD_ANY, Integer.MAX_VALUE));

    }
}

您应该尝试将 LoggingFeature 更改为 -

environment.jersey().register(new LoggingFeature(
      Logger.getLogger(LoggingFeature.class.getName()), Level.FINE,
             LoggingFeature.Verbosity.PAYLOAD_TEXT, null));

由于当前日志记录设置为 Level.OFF。该应用程序不会尝试记录任何内容,并且 filter implementation of ContainerRequestFilter and filter implementation of ContainerResponseFilter interfaces with the java.util.Logger within which the log(LogRecord) 方法中的 returns 被 Logger 的某个子类覆盖,该子类正在将级别修改为 ERROR。

我认为这个实现很差。同时期望更改为 Level.OFF 根本不应该记录任何内容。 (至少不低于ERROR

同时指定的详细程度是 LoggingFeature.Verbosity.PAYLOAD_TEXT 所以整个

Content of HTTP headers as well as entity content of textual media types is logged.

这是日志中 [ERROR] 消息后的详细信息。