希望我能找到一些帮助,以帮助我了解如何修复示例应用程序日志。
这是一个Jersey服务器,这是它的唯一资源:
package br.com.logplanning.jersey.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String showMessage() {
return "Hello from Brazil";
}
}
示例项目的结构是这样的:
这是pom.xml,我认为Jersey要求工作所有依赖项:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.logplanning.jersey</groupId>
<artifactId>hello-jersey-logging</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Jersey and LoggingFeatures</name>
<url>http://wwww.logplanning.com.br</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
<jersey.version>2.28</jersey.version>
</properties>
<build>
<finalName>hello-jersey-logging</finalName>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
</project>
泽西岛的ServletContainer是通过部署描述符初始化的(我无法通过代码开始):
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Contact - REST Service</display-name>
<servlet>
<servlet-name>Jersey and LoggingFeatures</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>br.com.logplanning.jersey.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey and LoggingFeatures</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
最后,项目具有用于配置LoggingFeature的此类:
package br.com.logplanning.jersey.resources;
import java.util.Enumeration;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("resources")
public class AppResourceConfig extends ResourceConfig {
private static final Logger LOG = Logger.getLogger(AppResourceConfig.class.getName());
public AppResourceConfig() {
// Registering a new LoggingFeature object
register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.ALL, LoggingFeature.Verbosity.PAYLOAD_ANY, Integer.MAX_VALUE));
LOG.info("===========> " + LoggingFeature.DEFAULT_LOGGER_NAME);
// Searching for all handlers and setting then to Level.ALL
LogManager manager = LogManager.getLogManager();
Enumeration<String> loggerNames = manager.getLoggerNames();
while (loggerNames.hasMoreElements()) {
String nextElement = loggerNames.nextElement();
Logger logger = manager.getLogger(nextElement);
if (logger.getHandlers().length > 0) {
Handler[] handlers = logger.getHandlers();
for (Handler h : handlers) {
Level oldLevel = h.getLevel();
if (!oldLevel.equals(Level.ALL))
h.setLevel(Level.ALL);
LOG.info("===========> " + nextElement + ": from " + oldLevel + " to " + h.getLevel());
}
}
}
}
}
}
问题在于调用端点时没有记录请求和响应。
正如您在上面所看到的,我试图确保无论如何都正确设置记录器。
我认为它的处理程序已经到位,但是由于某些原因,Jersey没有发送任何内容。
请您澄清一下。
答案 0 :(得分:0)
我认为它的处理程序已经到位,但是由于某些原因,Jersey没有发送任何内容。
记录器树是动态的,并将在代码要求创建记录器时将记录器附加到树上。在配置代码创建记录器之前,您的配置代码可能正在运行。尝试将根记录器级别设置为ALL,因为始终创建该记录器。这是通过在代码中添加以下内容来完成的:
Logger.getLogger("").setLevel(Level.ALL);
接下来,验证logger level changes have been applied to your logger tree and handlers。在大多数情况下,您还可以使用JConsole在运行时检查记录器树。
如果您知道记录请求和响应的记录器的名称,则可以通过使用适当的参数调用Logger::getLogger来创建对那些记录器的“私有静态最终”引用。
答案 1 :(得分:0)
jmehrens,感谢您的帮助,现在一切正常! 首先,我摆脱了DD(web.xml)初始化。 web.xml文件已减少到其最小内容,如下所示:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Contact - REST Service</display-name>
</web-app>
所有初始化都放在AppResourceConfig类中:
package br.com.logplanning.jersey.resources;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("resources")
public class AppResourceConfig extends ResourceConfig {
private static final Logger LOGGING_INTERCEPTOR = Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME);
public AppResourceConfig() {
//Registering the resources package (no DD needed)
packages("br.com.logplanning.jersey.resources");
// Registering a new LoggingFeature object
//https://www.javaguides.net/2018/06/jersey-rest-logging-using-loggingfeature.html
register(new LoggingFeature(LOGGING_INTERCEPTOR, Level.INFO,
LoggingFeature.Verbosity.PAYLOAD_ANY, Integer.MAX_VALUE));
// https://stackoverflow.com/questions/44882648/logging-not-showing/44888306#44888306
Logger.getLogger("").setLevel(Level.INFO);
}
}
解决方案的一部分是纠正记录器的层次结构设置,另一部分是关于初始化日志拦截以及资源包注册的操作。