log4j.xml日志记录在spring控制器中无法正常工作?

时间:2013-04-02 09:21:40

标签: java spring logging spring-mvc log4j

我已将log4j.xml放入WEB-INF/classes/log4j.xml

我的log4j.xml是,

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" > -->

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="Threshold" value="INFO" />
        <param name="MaxFileSize" value="512KB" />
        <param name="MaxBackupIndex" value="10" />
        <param name="File" value="F:/Spring_logs/pointel_Aop.log" />
        <!-- <param name="File" value="${webapp.root}/WEB-INF/logs/helloworld.log" /> -->
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
        </layout>
    </appender>


    <!--sets the priority log level for org.springframework -->
    <logger name="org.springframework">
        <level value="info" />
    </logger>


    <!--sets the priority log level for com.pointel.EM.controllers -->
    <logger name="com.pointel.EM.controllers">
        <level value="debug" />
    </logger>


    <!--sets the default priority log level -->
    <root>
        <priority value="info"></priority>
        <appender-ref ref="fileAppender" />
    </root>

</log4j:configuration>

在我的web.xml

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

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

在我的controller

@Controller
public class PageController {

    Logger logger = Logger.getLogger(PageController.class);

    @RequestMapping(value="index.html" , method=RequestMethod.GET)
    public String indexPage(){
        //logger.log(Level.DEBUG,"Entered in the GET of index.html");
        logger.trace("Trace..............");
        logger.debug("Debug...............");

        System.out.println("Index Page............");
        return "Main";
    }
}

将在log4j.xml的指定位置创建日志文件。但日志信息不会打印在日志文件中。

在创建的日志文件中

2013-04-02 14:36:32,945 INFO  DispatcherServlet:301 FrameworkServlet 'dispatcher': initialization started 
2013-04-02 14:36:32,976 INFO  XmlWebApplicationContext:456 Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Apr 02 14:36:32 GMT+05:30 2013]; root of context hierarchy 
2013-04-02 14:36:33,023 INFO  XmlBeanDefinitionReader:315 Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 
2013-04-02 14:36:33,367 INFO  DefaultListableBeanFactory:549 Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@171f189: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,pageController,viewResolver,messageSource]; root of factory hierarchy 
2013-04-02 14:36:33,445 INFO  DefaultAnnotationHandlerMapping:411 Mapped URL path [/index.html] onto handler [com.pointel.EM.controllers.PageController@11db6bb] 
2013-04-02 14:36:33,445 INFO  DefaultAnnotationHandlerMapping:411 Mapped URL path [/Main.jsp] onto handler [com.pointel.EM.controllers.PageController@11db6bb] 
2013-04-02 14:36:33,445 INFO  DefaultAnnotationHandlerMapping:411 Mapped URL path [/addEmployee.html] onto handler [com.pointel.EM.controllers.PageController@11db6bb] 
2013-04-02 14:36:33,445 INFO  DefaultAnnotationHandlerMapping:411 Mapped URL path [/listEmployees.html] onto handler [com.pointel.EM.controllers.PageController@11db6bb] 
2013-04-02 14:36:33,476 INFO  Version:56 Hibernate Validator 4.0.2.GA 
2013-04-02 14:36:33,476 INFO  DefaultTraversableResolver:89 Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
2013-04-02 14:36:33,632 INFO  DispatcherServlet:320 FrameworkServlet 'dispatcher': initialization completed in 687 ms 
将在控制台中打印

Index page ........以确认控制器的输入。

任何人都可以帮助在日志文件中打印日志信息。

肯定赞赏好的答案。谢谢。

1 个答案:

答案 0 :(得分:0)

可能没有应用控制器的设置:

<!--sets the priority log level for com.pointel.EM.controllers -->
<logger name="com.pointel.EM.controllers">
    <level value="debug" />
</logger>

查看是否将通话从debug更改为info

@RequestMapping(value="index.html" , method=RequestMethod.GET)
public String indexPage(){
    logger.info("Info..............");

    System.out.println("Index Page............");
    return "Main";
}