这是使用(或者更确切地说是尝试使用)servlet 2.5和log4j 2.1
行logger.debug(“test”)yield无法解析符号调试。
我有点失落,原因是因为我已经跟踪了所有的文档?
package myPkg;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.web.WebLoggerContextUtils;
@Path("myresource")
public class MyResource {
final Logger logger = LogManager.getLogger(MyResource.class);
logger.debug("test");
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
我已根据文档在web.xml中实现了更改,相关摘录如下:
<!-- log4j-->
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!-- end log4j-->
我的log4j的pom.xml看起来像
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
</dependency>
编译时间错误:
(18,17)java:期待 (18,18)java:非法启动类型
答案 0 :(得分:1)
代码“logger.debug(”test“);”应该在方法,构造函数或至少一个静态块中,如下所示:
public class A {
{
logger.debug("test");
}
}