2017-03-17 43 views
1

我在run(Configuration configuration, Environment environment)方法添加此行Dropwizard Jersey記錄請求/響應爲ERROR,但爲什麼?

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

看着和this

我只得到的東西,在如果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)); 

    } 
} 

回答

2

您應該嘗試改變LoggingFeature到 -

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

由於當前日誌記錄設置爲Level.OFF。該應用程序不會嘗試記錄任何內容,並從filter implementation of ContainerRequestFilterfilter implementation of ContainerResponseFilter界面返回java.util.Logger,其中log(LogRecord)方法被Logger的某個子類重寫,該子類將等級修改爲ERROR。

我會第二次認爲這個實現被認爲是窮人。期待在同一時間更改爲Level.OFF不應記錄任何內容。 (至少不是下ERROR

同時指定的冗長是LoggingFeature.Verbosity.PAYLOAD_TEXT所以HTTP標頭以及文本媒體 類型的實體內容的整個

內容被記錄。

這是您的[錯誤]消息在日誌後的詳細信息。

+1

感謝它的幫助,以及我不能讓我的頭周圍,日誌產生時設置爲「關」,但沒有產生時,設置爲「全部」。我沒有想到嘗試'FINE'。試圖找出如何記錄從特定用戶調用特定請求的時間。但非常感謝解釋@nullpointer – TheLearner

+0

@TheLearner目前我不太確定這屬於哪裏。但是這應該作爲意外的行爲(bug)引發到所涉及的任何一個庫中。 – nullpointer

+0

我會盡快報告 – TheLearner

相關問題