如何将Logger映射到spring J2ee应用程序?

时间:2015-02-16 16:58:42

标签: spring java-ee log4j

我有一个带有Spring MVC的简单J2ee应用程序。现在我想插入一个记录器文件。所以我在springmvc-servlet中插入了这段代码:

<bean id="lo4gjConfigurator" class="com.springmvcapp.log.LoggerFactory">
       <property name="logReInit">
        <value>@logReInit@</value>
       </property>  
       <property name="fileName">
        <value>log4j.xml</value>
       </property> 
    </bean>

但如果我在服务器启动时尝试加载页面,则会出现此错误

javax.servlet.ServletException: Servlet.init() for servlet springmvcapp threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

root cause

org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.springmvcapp.log.LoggerFactory] for bean with name 'lo4gjConfigurator' defined in ServletContext resource [/WEB-INF/springmvcapp-servlet.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
    org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1269)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:581)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1335)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:901)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:652)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:600)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:666)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:519)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:460)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

编辑: 这是log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="ROOT_APPENDER" class="capacita.apache.log4j.RollingFileAppender">
        <param name="file" value="@log4j.logs@/pcs.log"/>
        <param name="MaxFileSize" value="50MB"/>
        <param name="MaxBackupIndex" value="5"/>
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss} | %X{serverInstance} | %p | %X{sessionId} | %C{1} | %M | %m%n"/>
        </layout>
    </appender>

    <appender name="ERROR_APPENDER" class="capacita.apache.log4j.RollingFileAppender">
        <param name="file" value="@log4j.logs@/err_pcs.log"/>
        <param name="MaxFileSize" value="50MB"/>
        <param name="MaxBackupIndex" value="5"/>
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss} | %X{serverInstance} | %p | %X{sessionId} | %m%n"/>
        </layout>
    </appender>

    <appender name="LOGIN_APPENDER" class="com.springmvcapp.log.RollingFileAppender">
        <param name="file" value="@log4j.logs@/login-pcs.log"/>
        <rollingPolicy class="com.springmvcapp.log.TimeBasedRollingPolicy">
            <param name="fileNamePattern" value="@log4j.logs@/log-pcs-%d{yyyyMMdd}.log"/>
        </rollingPolicy>
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss}|%m%n"/>
        </layout>
    </appender>

    <appender name="CONSOLE_APPENDER" class="capacita.apache.log4j.ConsoleAppender">
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{HH:mm:ss}] [%p] [%C{1}].[%M](%L) %m%n"/>
        </layout>
    </appender>

    <appender name="AXIS_APPENDER" class="capacita.apache.log4j.RollingFileAppender">
        <param name="file" value="@log4j.logs@/axis_pcs.log"/>
        <param name="MaxFileSize" value="50MB"/>
        <param name="MaxBackupIndex" value="5"/>
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss} | %X{serverInstance} | %p | %X{sessionId} | %C{1} | %M | %m%n"/>
        </layout>
    </appender>

    <appender name="DAEMON_APPENDER" class="capacita.apache.log4j.RollingFileAppender">
        <param name="file" value="@log4j.logs@/daemon_pcs.log"/>
        <param name="MaxFileSize" value="50MB"/>
        <param name="MaxBackupIndex" value="5"/>
        <layout class="capacita.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss} | %X{serverInstance} | %p | %X{sessionId} | %C{1} | %M | %m%n"/>
        </layout>
    </appender>

    <category name="log4j.logger.org.apache.axis.SOAPPart">
        <priority value="DEBUG"/>
        <appender-ref ref="AXIS_APPENDER"/>
    </category>

    <logger name="AXIS">
        <level value="DEBUG"/>
        <appender-ref ref="AXIS_APPENDER"/>
    </logger>


    <logger name="org.springframework">
        <level value="ERROR"/>
        <appender-ref ref="ROOT_APPENDER"/>
    </logger>


    <logger name="ERROR">
        <level value="ERROR"/>
        <appender-ref ref="ERROR_APPENDER"/>
        <!--appender-ref ref="CONSOLE_APPENDER"/-->
    </logger>

    <logger name="ROOT">
        <level value="DEBUG"/>
        <level value="INFO"/>
        <appender-ref ref="ROOT_APPENDER"/>
        <appender-ref ref="CONSOLE_APPENDER"/>
        <!--appender-ref ref="CONSOLE_APPENDER"/ -->
    </logger>

    <logger name="LOGIN">
        <level value="DEBUG"/>
        <appender-ref ref="LOGIN_APPENDER"/>
        <!--appender-ref ref="CONSOLE_APPENDER"/-->
    </logger>

    <logger name="DAEMON">
        <level value="DEBUG"/>
        <appender-ref ref="DAEMON_APPENDER"/>
        <!--appender-ref ref="CONSOLE_APPENDER"/-->
    </logger>
</log4j:configuration>

2 个答案:

答案 0 :(得分:1)

以spring为例,你有src / main / java,在其他源文件夹中你有src / main / resources,你必须在你的log4j.xml中发布你的log4j.xml

答案 1 :(得分:0)

尝试使用logback.xml很容易实现。  http://logback.qos.ch/reasonsToSwitch.html