温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java log4j使用

发布时间:2020-08-05 18:27:02 来源:网络 阅读:999 作者:zjy1002261870 栏目:开发技术
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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI