Reputation: 976
Can you help me spotting out the missing step for my configuration?
I'm trying to add logger to my very simple web application: in order to do so I'm using log4j2 (beta9).
I wrote down my log4j2.xml
as follows
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} /%L/[%M] - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="prova.Hello" level="trace" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="servletstest.TestLogger" level="trace" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
and I put it in the WEB-INF folder.
I have then a simple servlet which does the following
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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestLogger</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet TestLogger at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
logger.error("error");
logger.info("info");
logger.trace("trace");
logger.debug("debug");
} finally {
out.close();
}
As you can see it is just a try to see if the logger works as expected, but it does not. By reading here web app I don't need to configure the web.xml of my application because it runs servlet 3.0, infact if I try to add the configuration I get the following error
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.
But it seems not to load the log4j2 configuration file, since the output I excpect is similar to
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
instead I get only
10:57:55.490 [http-bio-8084-exec-5] ERROR servletstest.TestLogger - error
it looks like is not able to load the xml file and it sets by default the logger level to error only. In the same web page is written
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.
so I edited the catalina.properties becasuse I'm running tomcat 7.0.41 and I removed the log4j*.jar from the jartoskip
property.
What am I doing wrong?
by the way in the web.xml I have this
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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_2_5.xsd">
<display-name>test</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>prova</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>TestLogger</servlet-name>
<servlet-class>servletstest.TestLogger</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestLogger</servlet-name>
<url-pattern>/TestLogger</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
</listener>
</web-app>
You can notice that version
is 2.5 and not properly 3.0 is perhaps this affecting me somehow? or jersey configuration?
EDIT:
I tried again and changed the web.xml
to the following in order to have a web app version of 3.0 but it does not load the configuration.. what am I doing wrong?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
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">
<servlet>
<servlet-name>Prova</servlet-name>
<servlet-class>test.Prova</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Prova</servlet-name>
<url-pattern>/Prova</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
thanks in advance for the help.
Upvotes: 4
Views: 6409
Reputation: 1103
If you are using eclipse ide for developing purpose you can put the log4j2.xml file in src folder.
Upvotes: 1
Reputation: 33093
As I guessed in the comments, it needs to be put in WEB-INF/classes
, not WEB-INF
.
Upvotes: 4