[英]Configuring Log4j2 in web application using log4j-web.jar
I am trying to configure log4j2 in my webapplication by following some tutorials. 我正在尝试通过遵循一些教程在Web应用程序中配置log4j2。 I am working with glassfish 4.1.1 server and servlet version 3.1. 我正在使用glassfish 4.1.1服务器和Servlet 3.1版。 I am able to configure the logging feature with the below configuration: 我可以使用以下配置来配置日志记录功能:
log4j.properties 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
the properties file is placed in src/main/resources folder 属性文件放置在src / main / resources文件夹中
web.xml 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>
the context param that defines file location is commented out. 注释掉定义文件位置的上下文参数。 probably log4j looks by default for a file with name log4j.properties. 默认情况下,log4j可能会查找名称为log4j.properties的文件。 However just wanted to know if its the right way to specify the file location. 但是只是想知道它是否是指定文件位置的正确方法。
Also I am not sure from which jar is org.apache.logging.log4j.web.Log4jServletContextListener being referenced. 另外,我不确定从哪个jar引用org.apache.logging.log4j.web.Log4jServletContextListener。 I searched all jar files but could not locate this class. 我搜索了所有jar文件,但找不到此类。
LoginServlet.java 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);
}
}
pom.xml dependency for log4j log4j的pom.xml依赖项
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
All this works fine and I get logs in both console as well as file. 所有这些工作正常,我在控制台和文件中都获得了日志。 But when I try to change the configuration as per the following links : 但是,当我尝试按照以下链接更改配置时:
https://logging.apache.org/log4j/2.x/manual/webapp.html#Servlet-3.0 https://logging.apache.org/log4j/2.x/maven-artifacts.html https://logging.apache.org/log4j/2.x/manual/webapp.html#Servlet-3.0 https://logging.apache.org/log4j/2.x/maven-artifacts.html
Like instead of dependency specified earlier I switch to 喜欢而不是之前指定的依赖项,我切换到
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.6.2</version>
</dependency>
log4j-api and log4j-core are added as transitive dependencies with log4j-web.jar 将log4j-api和log4j-core与log4j-web.jar添加为传递依赖项
other changes I made in LoginServlet.java is because of the compile time error I was getting after switching to log4j-web.jar which is as below : 我在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);
the main changes are in the initialization of logger variable and the two imports. 主要变化在于logger变量的初始化和两个导入。 rest all configuration remained as it is. 其余所有配置保持原样。 Also i was able to locate the listener class specified in web.xml in log4j-web.jar in this case. 在这种情况下,我也能够在log4j-web.jar中找到web.xml中指定的侦听器类。 Still logging is not working with this configuartion. 仍然无法通过该配置记录日志。
Could some one please help me with it or tell me what I am doing wrong here ? 有人可以帮我吗,或者告诉我我在做什么错?
Ok, after a little bit of more research and re-reading of the links I refered to earlier I finally managed to configure log4j2 in my web application and now I am getting logs as expected. 好的,经过更多的研究和重新阅读了我之前提到的链接,我终于设法在Web应用程序中配置了log4j2,现在我可以按预期获取日志了。 Now would like to mention what I was doing wrong. 现在想提一提我做错了什么。
As stated in the link https://logging.apache.org/log4j/2.x/manual/webapp.html 如链接https://logging.apache.org/log4j/2.x/manual/webapp.html中所述
I need not register Log4jServletContextListener class in my deployment descriptor if I am using servlet 3.0 or above, it needs to be registered only in servlet 2.5 and below. 如果我使用的是Servlet 3.0或更高版本,则无需在部署描述符中注册Log4jServletContextListener类,它只需要在Servlet 2.5或更低版本中注册。 Also I need not provide the file location if I am adhering to the naming convention of log4j2. 另外,如果我遵循log4j2的命名约定,则无需提供文件位置。
Another change instead of log4j2.properties now I am using log4j2.xml as the format for properties file in log4j2 is different then that used in log4j1. 现在,我使用的是log4j2.xml,而不是log4j2.properties,因为log4j2中的属性文件格式与log4j1中使用的格式不同。 Thanks to the answers given in the link below which helped me figure this out. 感谢下面链接中给出的答案,这些答案帮助我弄清楚了这一点。 Log4j 2 doesn't support log4j.properties file anymore? Log4j 2不再支持log4j.properties文件?
For the current format supported refer link https://logging.apache.org/log4j/2.0/manual/configuration.html#Properties 有关当前支持的格式,请参阅链接https://logging.apache.org/log4j/2.0/manual/configuration.html#Properties
Below is how my deployment descriptor and log4j configuartion file looks now : 下面是我的部署描述符和log4j配置文件现在的外观:
web.xml 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 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.