Eclipse+Servlet 2.5 的Struts2项目使用Log4j2

时间:2022-02-16 21:52:00

在J2EE中如何使用Log4j2可以查阅官方文档:http://logging.apache.org/log4j/2.x/manual/webapp.html

以下是一个简单的DEMO:

1、首先创建一个名为Log4j2InJ2EEDemo的项目,配置如下:

Eclipse+Servlet 2.5 的Struts2项目使用Log4j2

2、导入struts2相关的包,并进行基础配置,参考:http://blog.csdn.net/axwolfer/article/details/40057941

3、导入log4j2相关的包,在src项目添加log4j2.xml配置文件,配置如下:

配置中我们有两个Appender,一个时标准的系统输出,一个是滚动文件,注意这里滚动文件我设置了"logs/demorolling.log",用这个配置时,在Eclipse中调试,最终的日志会输出到Eclipse的安装目录下,在安装目录下会创建logs文件夹

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>


<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %M-%L- %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/demorolling.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="2"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.log4j2inj2eedemo" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>

</Configuration>

4、创建一个名为com.log4j2inj2eedemo.Demo的Action,代码如下:

package com.log4j2inj2eedemo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;


public class Demo extends ActionSupport {
private static final Logger logger = LogManager.getLogger(Demo.class);

/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
logger.info("This is just a test.");
return SUCCESS;
}
}

5、添加一个名为index.jsp的页面,页面内容随便写,如:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>This is just a test.</p>
</body>
</html>
6、在sturts.xml中配置demo和index.jsp的对应关系:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

<package name="default" extends="struts-default">
<action name="demo" class="com.log4j2inj2eedemo.Demo">
<result name="success">/index.jsp</result>
</action>
</package>

</struts>

7、在web.xml中配置我们的Log4j配置文件及log4j的过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Log4j2InJ2EEDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>false</param-value>
</context-param>

<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/classes/log4j2.xml</param-value>
</context-param>

<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>
</web-app>

8、最终项目结构如下:

Eclipse+Servlet 2.5 的Struts2项目使用Log4j2

9:、启动调试,在浏览器地址栏输入:http://localhost:8080/Log4j2InJ2EEDemo/demo.action

查看Eclipse安装目录下新创建的logs文件夹及记录的日志:

Eclipse+Servlet 2.5 的Struts2项目使用Log4j2

10、把项目导出成WAR到tomcat的webapps目录下,重启tomcat系统会自动部署Log4j2InJ2EEDemo项目,启动完成后,在浏览器地址栏重新输入http://localhost:8080/Log4j2InJ2EEDemo/demo.action,然后去查找日志的时候找不到日志了,我使用的是win8系统,这种情况下修改log4j.xml,把RollingFile的fileName属性修改为fileName="/logs/demorolling.log"然后重新导出WAR,重新启动tomcat并浏览Index页面,此时在C盘根目录下便可以找到logs/demorolling.log文件