WildFly 8和Log4j 2的log4j-web模块

时间:2015-02-10 17:39:38

标签: java logging slf4j wildfly log4j2

我正在开发一个在WildFly上运行的Web应用程序,它正在使用SLF4J和Log4j 2作为日志记录系统。在关于Log4j 2的Apache页面上,我读到了在Web应用程序(Using Log4j 2 in Web Applications)中使用log4j-web模块的优点,所以我添加了它,然后WildFly拒绝部署(这就是我评论它的原因)在下面的列表中。)

所以,这是我的问题:是否建议将log4j-web模块与WildFly一起使用?如果是,我该如何设置它以使用WindFly?

以下是相关列表:

web应用\ WEB-INF \类\ log4j2.xml

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

<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="myFile.log">
            <PatternLayout pattern="%d{yyy-MM-dd 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>

web应用\ WEB-INF \ JBoss的部署-structure.xml

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

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

父pom.xml

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

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    *snip*

    <properties>
        <java.version>1.8</java.version>
        <slf4j.version>1.7.10</slf4j.version>
        <log4j.version>2.1</log4j.version>
        <maven.plugin.compiler.version>3.2</maven.plugin.compiler.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- SLF4J -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <!-- Apache Log4j API -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
            </dependency>

            <!-- Apache Log4j SLF4J Binding -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>${log4j.version}</version>
            </dependency>

            <!-- Apache Log4j Core -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    *snip*

</project>

后端pom.xml

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

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    *snip*

    <dependencies>
        <!-- SLF4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>

        <!-- Apache Log4j API -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>

        <!-- Apache Log4j SLF4J Binding -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>

        <!-- Apache Log4j Core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
    </dependencies>
</project>

WebApp pom.xml

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

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    *snip*

    <properties>
        <servlet.version>3.1.0</servlet.version>

        <maven.plugin.war.version>2.6</maven.plugin.war.version>
        <maven.plugin.wildfly.version>1.0.2.Final</maven.plugin.wildfly.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Backend dependency -->
            <dependency>
            *snip*
            </dependency>

            <!-- Apache Log4j Web -->
            <!--<dependency>-->
            <!--<groupId>org.apache.logging.log4j</groupId>-->
            <!--<artifactId>log4j-web</artifactId>-->
            <!--<version>${log4j.version}</version>-->
            <!--<scope>runtime</scope>-->
            <!--</dependency>-->

            <!-- Java Servlet API -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${servlet.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Backend dependency -->
        <dependency>
        *snip*
        </dependency>

        <!-- Apache Log4j Web -->
        <!--<dependency>-->
        <!--<groupId>org.apache.logging.log4j</groupId>-->
        <!--<artifactId>log4j-web</artifactId>-->
        <!--</dependency>-->

        <!-- Java Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <!-- Maven WAR Plugin -->
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven.plugin.war.version}</version>
                </plugin>

                <!-- WildFly Maven Plugin -->
                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <version>${maven.plugin.wildfly.version}</version>

                    <configuration>
                        <!-- Server credentials from Maven's settings.xml -->
                        <id>WildFlyServer</id>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <!-- WildFly Maven Plugin -->
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:0)

这是common bug wildfly 8.x,只有wildfly 9.x

的修正

Log4j2 2.1以后会出现wildFly 8.x.

的部署错误

Log4j2 2.0.2没有此错误,但有其他阻止程序错误(LOG4J2-832)