Dropwizard Jersey记录请求/响应为ERROR,但为什么?

时间:2017-03-17 08:29:17

标签: jersey dropwizard java.util.logging

我添加了这一行

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

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

如果Level设置为OFF且我的所有请求/响应消息都记录为ERROR,那么我只会在日志中获取内容,但为什么会出错?

以下是日志示例:

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

我的资源类:

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

    public HelloWorldResource() { }

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

        return Response.ok().build();

    }
}

我的主要应用类:

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));

    }
}

1 个答案:

答案 0 :(得分:2)

您应尝试将environment.jersey().register(new LoggingFeature( Logger.getLogger(LoggingFeature.class.getName()), Level.FINE, LoggingFeature.Verbosity.PAYLOAD_TEXT, null)); 更改为 -

Level.OFF

由于当前日志记录设置为Logger。应用程序不会尝试记录任何内容,并从filter implementation of ContainerRequestFilterfilter implementation of ContainerResponseFilter接口返回java.util.Logger,其中log(LogRecord)方法被Level.OFF的某些子类覆盖这是将级别修改为ERROR。

我认为这个实现被认为是穷人。同时期望更改为ERROR不应该记录任何内容。 (至少不在{{1}}下)

同时指定的详细程度为LoggingFeature.Verbosity.PAYLOAD_TEXT,因此整个

  

HTTP标头的内容以及文本媒体的实体内容   记录类型。

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