Struts2和Log4j集成示例项目

时间:2020-02-23 14:36:05  来源:igfitidea点击:

今天,我们将学习如何在Struts2 Web应用程序中使用Log4j日志记录。
我们将通过一个简单的项目来学习它。

配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Log4jExample</display-name>

	<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>

</web-app>

Web应用程序部署描述符很简单,并且配置为使用Struts2框架。

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Struts2Log4jExample</groupId>
	<artifactId>Struts2Log4jExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.15.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

需要注意的重要一点是我们将在项目中使用的struts2-core和log4j依赖项。

struts.xml

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

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"https://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.convention.result.path" value="/"></constant>
	<package name="user" namespace="/" extends="struts-default">
		<action name="home" class="com.theitroad.struts2.actions.HomeAction">
			<result name="success">/home.jsp</result>
		</action>

	</package>

</struts>

具有操作和结果页面映射的简单的struts配置文件。

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration xmlns:log4j="https://jakarta.apache.org/log4j/"
	debug="false">
	<appender name="log4jexample" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${catalina.home}/logs/example.log" 
		<param name="Append" value="true" 
		<param name="ImmediateFlush" value="true" 
		<param name="MaxFileSize" value="20MB" 
		<param name="MaxBackupIndex" value="10" 
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" 
		</layout>
	</appender>

	<logger name="com.theitroad" additivity="false">
		<level value="DEBUG" 
		<appender-ref ref="log4jexample" 
	</logger>

	<root>
		<level value="debug" 
		<appender-ref ref="log4jexample" 
	</root>

</log4j:configuration>

简单的log4j xml配置文件,它应该位于classpath中,以便它位于WEB-INF/classes目录中,类似于struts配置文件。

使用log4j日志记录的操作类

package com.theitroad.struts2.actions;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(HomeAction.class);
	
	public String execute(){
		logger.info("inside HomeAction execute method");
		return SUCCESS;
	}
}

Struts2自动为我们配置log4j,我们可以直接在操作类中使用它。
请注意,Action类中的Logger初始化以及我们在execute方法中使用的日志记录。

导出并运行该项目后,将在example.log文件中获取以下日志。

23450 [http-bio-8080-exec-4] INFO  com.theitroad.struts2.actions.HomeAction  - inside HomeAction execute method
26346 [http-bio-8080-exec-6] INFO  com.theitroad.struts2.actions.HomeAction  - inside HomeAction execute method