我添加了这一行
environment.jersey().register(new LoggingFeature(Logger
.getLogger(LoggingFeature.class.getName()), Level.OFF,
LoggingFeature.Verbosity.PAYLOAD_TEXT, null));
在我的run(Configuration configuration, Environment environment)
方法中查看这个和这个
如果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));
}
}
您应该尝试将LoggingFeature
更改为 -
environment.jersey().register(new LoggingFeature(
Logger.getLogger(LoggingFeature.class.getName()), Level.FINE,
LoggingFeature.Verbosity.PAYLOAD_TEXT, null));
由于当前日志记录设置为 Level.OFF
.应用程序不会尝试记录任何内容,并从ContainerRequestFilter
的过滤器实现和ContainerResponseFilter
java.util.Logger
接口的过滤器实现返回,其中log(LogRecord)
方法被Logger
的某个子类覆盖,该子类将级别修改为 ERROR。
我赞成认为这种实现被认为是糟糕的。同时期望更改为Level.OFF
根本不应该记录任何内容。(至少不低于ERROR
(
同时指定的详细程度LoggingFeature.Verbosity.PAYLOAD_TEXT
因此整个
HTTP 标头的内容以及文本媒体的实体内容 记录类型。
这是日志中 [错误] 消息之后的详细信息。