struts2 log4j、width = 286、height = 67

このチュートリアルでは、log4jフレームワークをStruts 2 Webアプリケーションと統合する方法について説明します。あなたがする必要があるのは、

  1. プロジェクトの依存関係として `log4j.jar`を含めます

  2. log4j.propertiesファイルを作成し、そのファイルのルートに配置します.

classpathをMavenと一緒に `resources`フォルダに入れます。

使用される技術とツール:

  1. Log4j 1.2.17

  2. Struts 2.3.16.3

  3. Maven 3

  4. Tomcat 6

  5. Eclipseケプラー4.3

1.プロジェクトディレクトリ

最終的なプロジェクトの構造を見直します。


strruts2-log4j-directory、width = 465、height = 502

プロジェクトの依存関係

Struts 2とlog4jの依存関係を宣言します:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/maven-v4__0__0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>Struts2</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Struts + Log4j Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jdk.version>1.7</jdk.version>
        <struts.version>2.3.16.3</struts.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>

        <!-- Struts 2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
        </dependency>

        <!-- Log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

    </dependencies>

    <build>
      <finalName>Struts2</finalName>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <wtpversion>2.0</wtpversion>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
      </plugins>
    </build>
</project>

3. log4j.properties

log4jプロパティファイルを作成し、それを `resources`フォルダに入れます。ステップ1を参照してください。

log4j.properties

# Root logger option
log4j.rootLogger=ERROR, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support rolling backup file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/mystruts2app.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

4. Struts 2アクションとロギング

ページを返す単純なアクションと、log4jでメッセージロギングを行う方法を示します。

WelcomeAction.java

package com.mkyong.common.action;

import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;

public class WelcomeAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

   //get log4j
    private static final Logger logger = Logger.getLogger(WelcomeAction.class);

    public String execute() throws Exception {

       //logs debug message
        if (logger.isDebugEnabled()) {
            logger.debug("execute()!");
        }

       //logs exception
        logger.error("This is Error message", new Exception("Testing"));

        return SUCCESS;

    }
}

5. Struts 2の設定

興味がある場合は、Struts 2の設定とJSPページを参照してください。

struts.xml

<?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.devMode" value="true"/>

    <package name="welcome" namespace="/" extends="struts-default">

        <action name="welcome" class="com.mkyong.common.action.WelcomeAction">
            <result name="success">pages/success.jsp</result>
        </action>

    </package>

</struts>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app__2__5.xsd"
    version="2.5">

    <display-name>Struts 2 Web Application</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>

pages/success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 + Log4j integration example</h1>

</body>
</html>

6.デモ

Struts 2 Webアプリケーションを実行し、ウェルカム・アクションにアクセスします。


URL:http://localhost:8888/Log4jAndStruts2/welcome


struts 2 log4jデモ、幅= 638、高さ= 338


6.1

すべてのログメッセージがコンソールに表示されます。


strruts2-log4j-demo-eclipse-console、width = 626、height = 258


Figure:Eclipseコンソール


6.2

さらに、ログファイルがTomcatのlogsフォルダに作成されます。


strruts2-log4j-demo-file、width = 640、height = 336


Figure:D:\ apache-tomcat-6.0.37 \ logs \ mystruts2app.log

ソースコードをダウンロードする

ダウンロードする –

Log4jAndStruts2Example.zip

(20 KB)