如何使用log4j输出单个级别的log到指定文件

通过配置log4j.properties实现输出单个级别的log到指定文件。

顺便把关联的配置文件都贴出来
环境:Struts2 + Spring3

web.xml
<!-- 2011.10.20 配置log start -->
<!--由Sprng载入的Log4j配置文件位置-->
<context-param>
	<param-name>log4jConfigLocation</param-name>
	<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond--> 
<context-param>
	<param-name>log4jRefreshInterval</param-name>
	<param-value>60000</param-value>
</context-param>
<!--Spring log4j 监听器-->
<listener>
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 2011.10.20 配置log end -->


log4j.properties
log4j.logger.BUSINESS=DEBUG,A7

log4j.appender.A7=org.apache.log4j.RollingFileAppender
log4j.appender.A7.Threshold=INFO
log4j.appender.A7.File=${catalina.base}/logs/business-info.log
log4j.appender.A7.DatePattern='.'yyyy-MM-dd
log4j.appender.A7.Append=true
log4j.appender.A7.Encoding=gbk
log4j.appender.A7.MaxFileSize=5000KB
log4j.appender.A7.MaxBackupIndex=1000
log4j.appender.A7.layout=org.apache.log4j.PatternLayout
log4j.appender.A7.layout.ConversionPattern=%d %5p %c{1} %x - %m%n
#配置Filter 
#定义id为F1的拦截器,只接受INFO到ERROR
log4j.appender.A7.filter.F1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.A7.filter.F1.LevelMin=INFO
log4j.appender.A7.filter.F1.LevelMax=ERROR
#定义id为F2的拦截器,不接受WARN
log4j.appender.A7.filter.F2=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.A7.filter.F2.levelToMatch=WARN
log4j.appender.A7.filter.F2.acceptOnMatch=false


这么使用:
private static final Logger log = LoggerFactory.getLogger("BUSINESS");
log.debug(.....);

你可能感兴趣的:(单个级别,输出log到文件)