码迷,mamicode.com
首页 > 编程语言 > 详细

java log4j使用

时间:2019-01-31 12:56:20      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:end   play   war   ret   name   system   off   pat   isp   

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:\javalog\qc.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:\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

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:\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

java log4j使用

标签:end   play   war   ret   name   system   off   pat   isp   

原文地址:http://blog.51cto.com/6000734/2348128

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!