struts2 log4j_Struts2和Log4j集成示例项目

struts2 log4j

Sometime back I wrote an article about using log4j in web application and we utilized Servlet Context Listener to configure the log4j in web applications before we can use it.

有时我写了一篇有关在Web应用程序中使用log4j的文章,我们在使用Servlet Context Listener之前在Web应用程序中配置了log4j。

Today we will learn how to use Log4j logging in Struts2 web application. We will learn it with a simple project whose final structure looks like below image.

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

配置文件 (Configuration Files)

web.xml

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 application deployment descriptor is simple and configured to use Struts2 framework.

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

pom.xml

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>

Important point to note is the struts2-core and log4j dependency that we will use in the project.

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

struts.xml

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.journaldev.struts2.actions.HomeAction">
			<result name="success">/home.jsp</result>
		</action>

	</package>

</struts>

Simple struts configuration file with action and result pages mapping.

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

log4j.xml

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.journaldev" additivity="false">
		<level value="DEBUG" />
		<appender-ref ref="log4jexample" />
	</logger>

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

</log4j:configuration>

Simple log4j xml configuration file, it should be in the classpath so that it will be inside WEB-INF/classes directory, similar to struts configuration file.

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

使用log4j日志记录的操作类 (Action Class using log4j logging)

package com.journaldev.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 automatically configures the log4j for us and we can use directly in the action classes. Notice the Logger initialization in the action class and that we are using in execute method for logging.

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

Once we export this project and run it, we get following logs in example.log file.

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

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

That’s it, you can see how easy it is to use log4j in Struts2 web application.

就是这样,您可以看到在Struts2 Web应用程序中使用log4j多么容易。

翻译自: https://www.journaldev.com/2247/struts2-and-log4j-integration-example-project

struts2 log4j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值