Spring MVC app中JBoss 7.1.1 final的Log4j配置

时间:2014-12-05 10:30:35

标签: spring-mvc log4j jboss7.x

我正在使用jboss 7.1.1 final处理spring mvc应用程序。我需要使用log4j进行日志记录。以下是我的web.xml:

<?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"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>My projects</display-name>

    <servlet>
        <servlet-name>route</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>route</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/resources/log4j.xml</param-value>
    </context-param>
     <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
</web-app>

以下是我的调度员servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/" />
        <context:component-scan base-package="com.bizmerlin.scm.controller" />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
</beans>

以下是我在WEB-INF / resources目录下的log4j.xml:

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="F:/LogFile.log" />
        <param name="MaxFileSize" value="5MB" />
        <param name="MaxBackupIndex" value="50" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />
        </layout>

    </appender>

    <appender name="htmlFileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="F:/LogFile.html" />
        <param name="MaxFileSize" value="5MB" />
        <param name="MaxBackupIndex" value="50" />
        <layout class="org.apache.log4j.HTMLLayout">
            <param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />
        </layout>

    </appender>

    <appender name="stdOut" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />
        </layout>
    </appender>

    <!-- package level log levels can be define -->
    <category name="com.res.common.controller">
        <priority value="debug" />
    </category>


    <!-- Root Logger trace/debug/info/warn/error/fatal/off> -->
    <root>
        <priority value="info" />
        <appender-ref ref="fileAppender" />
        <appender-ref ref="htmlFileAppender" />
        <!-- remove stdOut to disable printing logs in server console -->
        <appender-ref ref="stdOut" />

    </root>

</log4j:configuration>

我在jboss中遇到以下错误:

11:14:55,484 ERROR [stderr] (Finalizer) log4j:ERROR Failed to flush writer,
11:14:55,514 ERROR [stderr] (Finalizer) java.io.IOException: Stream Closed
11:14:55,516 ERROR [stderr] (Finalizer)         at java.io.FileOutputStream.writeBytes(Native Method)
11:14:55,518 ERROR [stderr] (Finalizer)         at java.io.FileOutputStream.write(FileOutputStream.java:345)
11:14:55,519 ERROR [stderr] (Finalizer)         at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)

LogFile.log文件中未创建日志。

我该怎么做才能纠正日志记录功能?

1 个答案:

答案 0 :(得分:0)

如果您选择在Jboss 7.1.1中使用log4j配置文件。

您必须按照以下步骤操作:

  1. 使用以下内容创建jboss-deployment-structure.xml 如果要部署EAR,请将其放在META-INF/目录中 或者在META-INF/WEB-INF/目录中,如果你是 部署WAR。
  2. <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
        <deployment>
            <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
            <exclusions>
                <module name="org.apache.log4j" />
            </exclusions>
        </deployment>
    </jboss-deployment-structure>
    
    1. 在您的帐号中包含log4j库和log4j配置文件 应用程序类路径
    2. 并删除

          <context-param>
              <param-name>log4jConfigLocation</param-name>
              <param-value>/WEB-INF/resources/log4j.xml</param-value>
          </context-param>
      

      另见:

      我希望这有帮助。