简体   繁体   中英

Log4j configuration on JBoss 7.1.1 final in Spring MVC app

I am working on spring mvc application using jboss 7.1.1 final. I need to use log4j for logging. Following is my 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>

Following is my dispatcher 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>

Following is my log4j.xml under WEB-INF/resources directory:

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

I am getting following error in 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)

Logs are not getting created in LogFile.log file.

What should I do to correct logging functionality?

If you choose to use a log4j configuration file in Jboss 7.1.1.

You have to follow these steps:

  1. Create a jboss-deployment-structure.xml with the following content and place it in the META-INF/ directory if you are deploying an EAR or in either the META-INF/ or WEB-INF/ directory if you are deploying a WAR.
<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. Include log4j libraries and log4j configuration file in your application classpath

And remove

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

See also:

I hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM