您好,登錄后才能下訂單哦!
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文件開始--------------------
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:\javalog\qc.log
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
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:\javalog\qc_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
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:\javalog\qc_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
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。