您能帮我找出配置中缺少的步骤吗?
我试图将记录器添加到我非常简单的Web应用程序中:为此,我使用了log4j2(beta9).
我写下了我的log4j2.xml如下
并将其放在WEB-INF文件夹中.
然后,我有一个简单的servlet,它可以执行以下操作
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("");
out.println("");
out.println("
");out.println("
Servlet TestLogger");out.println("");
out.println("
");out.println("
out.println("");
out.println("");
logger.error("error");
logger.info("info");
logger.trace("trace");
logger.debug("debug");
} finally {
out.close();
}
如您所见,这只是尝试查看记录器是否按预期工作,但事实并非如此.
通过在这里阅读web app,我不需要配置应用程序的web.xml,因为它运行的是Servlet 3.0,实际上,如果我尝试添加配置,则会出现以下错误
javax.servlet.UnavailableException: In a Servlet 3.0+ application, you must not define a log4jServletFilter in web.xml. Log4j 2 defines this for you automatically.
但是似乎不加载log4j2配置文件,因为我期望的输出类似于
10:57:55.490 [http-bio-8084-exec-5] ERROR servletstest.TestLogger - error
10:57:55.490 [http-bio-8084-exec-5] INFO servletstest.TestLogger - info
10:57:55.490 [http-bio-8084-exec-5] TRACE servletstest.TestLogger - trace
10:57:55.490 [http-bio-8084-exec-5] DEBUG servletstest.TestLogger - debug
相反,我只得到
10:57:55.490 [http-bio-8084-exec-5] ERROR servletstest.TestLogger - error
似乎无法加载xml文件,并且默认情况下将记录器级别设置为仅错误.
在同一网页中编写
In Tomcat 7 <7.0.43 you will need to change catalina.properties and remove “log4j*.jar” from the jarsToSkip property. You may need to do something similar on other containers if they skip scanning Log4j JAR files.
所以我编辑catalina.properties,因为我正在运行tomcat 7.0.41,并且从jartoskip属性中删除了log4j * .jar.
我究竟做错了什么?
顺便说一下,在web.xml中,我有这个
test
Jersey REST Service
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
prova
1
TestLogger
servletstest.TestLogger
Jersey REST Service
/rest/*
TestLogger
/TestLogger
org.apache.logging.log4j.core.web.Log4jServletContextListener
您会注意到版本是2.5,而不是3.0,也许这在某种程度上影响了我?还是球衣配置?
编辑:
我再次尝试将web.xml更改为以下内容,以使Web应用程序版本为3.0,但它不会加载配置..我在做什么错?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Prova
test.Prova
Prova
/Prova
30
先谢谢您的帮助.