我正在尝试通过以下教程在我的webapplication中配置log4j2。我正在使用glassfish 4.1.1服务器和servlet版本3.1。我可以使用以下配置配置日志记录功能:
log4j.properties
# Root logger option
log4j.rootLogger=INFO, consoleAppender, fileAppender
# debug level logger
log4j.logger.kumar.suraj.college.administration.login=DEBUG
# Redirect log messages to console
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.Target=System.out
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fileAppender.File=E:\\DEVELOPMENT\\JAVA\\web-logs\\web-college-administration\\applicationLogs.log
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
属性文件放在src / main / resources文件夹中
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>College Administration</display-name>
<!-- <context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/log4j.properties</param-value>
</context-param>is it required
-->
<!--from where is this class referenced in dependency without web -->
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>kumar.suraj.college.administration.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
定义文件位置的上下文参数被注释掉。可能log4j默认查找名为log4j.properties的文件。但是,只是想知道它是否是指定文件位置的正确方法。
此外,我不确定哪个jar是org.apache.logging.log4j.web.Log4jServletContextListener被引用。我搜索了所有jar文件但找不到这个类。
LoginServlet.java
package kumar.suraj.college.administration.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import kumar.suraj.college.administration.adduser.AddUserServlet;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// final static Logger logger = LogManager.getLogger(LoginServlet.class);
final static Logger logger = Logger.getLogger(LoginServlet.class);
public LoginServlet() {
super();
}
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
LoginServlet.logger.debug("debug level logging supported"); response.getWriter().append("Servedat:").append(request.getContextPath());
response.getWriter().append("Hello Suraj");
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
LoginServlet.logger.debug("debug level logging supported");
this.doGet(request, response);
}
}
log4j的pom.xml依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
所有这一切都很好,我在控制台和文件中都可以获得日志。但是当我尝试按照以下链接更改配置时:
https://logging.apache.org/log4j/2.x/manual/webapp.html#Servlet-3.0 https://logging.apache.org/log4j/2.x/maven-artifacts.html
与之前指定的依赖性不同,我转而使用
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.6.2</version>
</dependency>
log4j-api和log4j-core作为传递依赖项添加到log4j-web.jar
我在LoginServlet.java中做的其他更改是由于我在切换到log4j-web.jar后得到的编译时错误,如下所示: import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
final static Logger logger = LogManager.getLogger(LoginServlet.class);
// final static Logger logger = Logger.getLogger(LoginServlet.class);
主要的变化是logger变量和两个导入的初始化。休息所有配置保持原样。此外,我能够在log4j-web.jar中找到web.xml中指定的侦听器类。仍然记录不适用于此配置。
有人可以帮助我,或者告诉我这里我做错了什么?
答案 0 :(得分:3)
好的,经过一些研究并重新阅读我之前提到的链接后,我终于设法在我的Web应用程序中配置log4j2,现在我按预期获取日志。现在想提一下我做错了什么。
如链接https://logging.apache.org/log4j/2.x/manual/webapp.html
中所述如果我使用servlet 3.0或更高版本,我不需要在部署描述符中注册Log4jServletContextListener类,它只需要在servlet 2.5及更低版本中注册。如果我遵守log4j2的命名约定,我也不需要提供文件位置。
另一个更改而不是log4j2.properties现在我使用log4j2.xml,因为log4j2中属性文件的格式与log4j1中使用的格式不同。感谢下面链接中给出的答案,这些答案帮助我解决了这个问题。 Log4j 2 doesn't support log4j.properties file anymore?
对于支持的当前格式,请参阅链接https://logging.apache.org/log4j/2.0/manual/configuration.html#Properties
以下是我的部署描述符和log4j configuartion文件现在的样子:
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>College Administration</display-name>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>kumar.suraj.college.administration.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="E:\\DEVELOPMENT\\JAVA\\web-logs\\web-college-administration\\applicationLogs.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>