现在的位置: 首页 > 操作系统 > 正文

Spring的Log4J配置

2020年02月10日 操作系统 ⁄ 共 1113字 ⁄ 字号 评论关闭

默认情况下,Spring会在classpath下查找log4j.properties文件,我们直接将其放在一般项目的src下即可。如果使用Maven项目,则应该放置在main/Java/resources的目录下

WebApplicationContext

在web项目环境下,一般来说也是同样的配置即可。但是有种较为特殊的情况是,log4j的配置文件必须放在非classpath下,则需要在web.xml中单独配置

用log4jConfigLocation指定路径

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

用一个监听器加载配置

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

注:这个监听器必须配置在ContextLoaderListener才可以起作用

附:简单的log4j.properties

log4j.rootLogger=INFO, console, filelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.file=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.File=D:/acy.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.logger.org.springframework=WARN

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2017-03/141256.htm

以上就上有关Spring的Log4J配置的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.