【问题标题】:Dropwizard Jersey logging request/response as ERROR, but why?Dropwizard Jersey 将请求/响应记录为错误,但为什么呢?
【发布时间】:2017-03-17 08:29:16
【问题描述】:

我已经添加了这一行

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

    }
}

【问题讨论】:

    标签: jersey dropwizard java.util.logging


    【解决方案1】:

    您应该尝试将LoggingFeature 更改为 -

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

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

    我会同意这个实现被认为是糟糕的想法。同时期望更改为Level.OFF 根本不应该记录任何内容。 (至少不在ERROR下)

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

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

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

    【讨论】:

    • 感谢它的帮助,我也无法理解设置为OFF 时会生成日志,但设置为ALL 时不会生成任何日志。我没有想到尝试FINE。只是想弄清楚当给定用户调用特定请求时如何记录。但是非常感谢@nullpointer的解释
    • @TheLearner 目前我不太确定这属于哪里。但这应该作为所涉及的任何一个库的意外行为(错误)提出。
    • 我会尽快报告
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多