java log4j使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java log4j使用相关的知识,希望对你有一定的参考价值。
web.xml配置文件添加如下两个配置
<!-- log4jConfigLocation:log4j配置文件存放路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:META-INF/properties/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
</context-param>
applicationContext.xml 添加properties文件配置
<!--1 引入属性文件,在配置中占位使用 -->
<context:property-placeholder
location="classpath:META-INF/properties/*.properties" />
log4j文件开始--------------------
OFF FATAL ERROR WARN INFO DEBUG ALL
log4j.rootCategory=INFO, stdout , infolog,errorlog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %d{yyyy-MM-dd HH:mm:ss SSS} %p [%t] %C.%M(%L) | %m%n
#log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.R.File=F:javalogqc.log
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
output info log
log4j.logger.infolog = info,infolog
log4j.appender.infolog.Append = true
#log4j.appender.errorlog.Threshold = info
log4j.appender.infolog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.infolog.File = F:javalogqc_info.log
log4j.appender.infolog.layout = org.apache.log4j.PatternLayout
log4j.appender.infolog.layout.ConversionPattern = [%d{MM-dd HH:mm:ss}] [%p] [%c:%L] %m%n
output error log
log4j.logger.errorlog = error,errorlog
log4j.appender.errorlog.Append = true
log4j.appender.errorlog.Threshold = error
log4j.appender.errorlog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorlog.File = F:javalogqc_error.log
log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlog.layout.ConversionPattern = [%d{MM-dd HH:mm:ss}] [%p] [%c:%L] %m%n
log4j.logger.org.displaytag=ERROR
log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.apache.velocity=FATAL
log4j.logger.com.canoo.webtest=WARN
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.logicalcobwebs=WARN
log4j文件结束--------------------
?在rootLogger上指定level和appenderName;
?下面对每个appenderName的配置中,使用log4j.logger.appendName = levle,appendName 的格式来配置级别(例如:log4j.logger.infolog = info,infolog ) ,同时用Threshold来决定不同级别的日志
private Logger logger=LoggerFactory.getLogger(TestService.class);
public String getAppExtStr() {
System.out.println("appExtStr="+ appExtStr);
logger.info(String.format("appExtStr=%s", appExtStr));
logger.error("userid=10803060234");
return appExtStr;
}
日志输出例子:
[01-31 11:24:13] [ERROR] [com.skcc.chart.service.DefaultFileUploadService:24] userid=10803060234
以上是关于java log4j使用的主要内容,如果未能解决你的问题,请参考以下文章