log4j配置及使用
#ThisistheconfiguringforloggingdisplayedintheApplicationServer
#log4j.rootCategory=INFO,stdout,common-all,common-warn,common-error
log4j.rootLogger=INFO,stdout
##############appenderoptions###################
#org.apache.log4j.ConsoleAppender,
#org.apache.log4j.FileAppender,
#org.apache.log4j.DailyRollingFileAppender,
#org.apache.log4j.RollingFileAppender,
#org.apache.log4j.WriterAppender,
###################################
##############layoutoptions###################
#org.apache.log4j.HTMLLayout,
#org.apache.log4j.PatternLayout,
#org.apache.log4j.SimpleLayout,
#org.apache.log4j.TTCCLayout
###################################
#################################
#%mcodeinfomation
#%pDEBUG,INFO,WARN,ERROR,FATAL
#%rcostmilisecondfromstarttoprint
#%d{yyyMMMddHH:mm:ss,SSS}
#%nenter
#%lline
###################################
################################
#commonappenders
################################
#stdoutconfigure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.Encoding=GBK
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-allfileappender
log4j.appender.common-all=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-all.file=/renshi/logs/common-all.log
log4j.appender.common-all.layout=org.apache.log4j.PatternLayout
log4j.appender.common-all.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-warnfileappender
log4j.appender.common-warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-warn.file=/renshi/logs/common-warn.log
log4j.appender.common-warn.layout=org.apache.log4j.PatternLayout
log4j.appender.common-warn.threshold=WARN
log4j.appender.common-warn.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-errorfileappender
log4j.appender.common-error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-error.file=/renshi/logs/common-error.log
log4j.appender.common-error.layout=org.apache.log4j.PatternLayout
log4j.appender.common-error.threshold=ERROR
log4j.appender.common-error.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-daofileappender
log4j.appender.common-dao=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-dao.file=/renshi/logs/common-dao.log
log4j.appender.common-dao.layout=org.apache.log4j.PatternLayout
log4j.appender.common-dao.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-webfileappender
log4j.appender.common-web=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-web.file=/renshi/logs/common-web.log
log4j.appender.common-web.layout=org.apache.log4j.PatternLayout
log4j.appender.common-web.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#common-pageNotFoundfileappender
log4j.appender.common-pageNotFound=org.apache.log4j.DailyRollingFileAppender
log4j.appender.common-pageNotFound.file=/renshi/logs/common-pageNotFound.log
log4j.appender.common-pageNotFound.layout=org.apache.log4j.PatternLayout
log4j.appender.common-pageNotFound.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
################################
#projectappenders
################################
#project-daofileappender
log4j.appender.project-dao=org.apache.log4j.DailyRollingFileAppender
log4j.appender.project-dao.file=/renshi/logs/project-dao.log
log4j.appender.project-dao.layout=org.apache.log4j.PatternLayout
log4j.appender.project-dao.threshold=INFO
log4j.appender.project-dao.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#project-webfileappender
log4j.appender.project-web=org.apache.log4j.DailyRollingFileAppender
log4j.appender.project-web.file=/renshi/logs/project-web.log
log4j.appender.project-web.layout=org.apache.log4j.PatternLayout
log4j.appender.project-web.threshold=INFO
log4j.appender.project-web.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
#project-servicefileappender
log4j.appender.project-service=org.apache.log4j.DailyRollingFileAppender
log4j.appender.project-service.file=/renshi/logs/project-service.log
log4j.appender.project-service.layout=org.apache.log4j.PatternLayout
log4j.appender.project-service.threshold=INFO
log4j.appender.project-service.layout.conversionPattern=%d[%X{loginUserId}/%X{req.remoteAddr}/%X{req.id}-%X{entranceMethod}-%X{req.requestURIWithQueryString}]%-5p%c-%m%n
################################
#opensourceframeworkloggers
################################
#spring
log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUG,common-dao
#debuglogforibatis2.3
#log4j.logger.java.sql.Connection=DEBUG,common-dao
#log4j.logger.java.sql.Statement=INFO,common-dao
#log4j.logger.java.sql.PreparedStatement=INFO,common-dao
#log4j.logger.java.sql.ResultSet=INFO,common-dao
#debuglogforibatis3
log4j.logger.java.sql.Connection=DEBUG,common-dao
log4j.logger.java.sql.Statement=DEBUG,common-dao
log4j.logger.java.sql.PreparedStatement=DEBUG,common-dao
log4j.logger.java.sql.ResultSet=INFO,common-dao
#hibernate:ChangingthelogleveltoDEBUGwilldisplaySQLHibernategenerated
log4j.logger.org.hibernate=INFO,common-dao
log4j.logger.org.hibernate.SQL=ERROR
log4j.logger.org.hibernate.cache=ERROR
log4j.logger.net.sf.ehcache=ERROR
#springmvcpageNotFound
log4j.logger.org.springframework.web.servlet.PageNotFound=INFO,common-pageNotFound
#HibernateValidator
log4j.logger.org.apache.commons.validator.ValidatorResources=INFO
#log4jdbc,see:http://code.google.com/p/rapid-framework/wiki/log4jdbcfordetails
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO,common-dao
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
################################
#projectloggers
################################
log4j.logger.com.company.project.dao=INFO,project-dao
log4j.logger.com.company.project.service=INFO,project-service
log4j.logger.com.company.project.web=INFO,project-web
2.类中引用
publicclassLog4jTest{
privatestaticLoggerlogger=Logger.getLogger("Log4jTest");//参数一般为类名
publicstaticintchu(intnum1,intnum2){
try{
returnnum1/num2;
}catch(Exceptionex){
logger.error("操作错误",ex);
//logger.error("操作错误");
return0;
}
}
}