gpt4 book ai didi

java - log4j.xml 日志记录在 spring Controller 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 09:04:33 25 4
gpt4 key购买 nike

我已将 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 ....... 将在控制台中打印以确认 Controller 的输入。

谁能帮忙把日志文件中的日志信息打印出来

非常感谢好的答案。谢谢。

最佳答案

也许您的 Controller 的设置没有被应用:

<!--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";
}

关于java - log4j.xml 日志记录在 spring Controller 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com