log4j配置及使用
# This is the configuring for logging displayed in the Application Server
#log4j.rootCategory=INFO, stdout,common-all,common-warn,common-error
log4j.rootLogger=INFO, stdout
############## appender options ###################
#org.apache.log4j.ConsoleAppender,
#org.apache.log4j.FileAppender,
#org.apache.log4j.DailyRollingFileAppender,
#org.apache.log4j.RollingFileAppender,
#org.apache.log4j.WriterAppender,
###################################
############## layout options ###################
# org.apache.log4j.HTMLLayout,
# org.apache.log4j.PatternLayout,
# org.apache.log4j.SimpleLayout,
# org.apache.log4j.TTCCLayout
###################################
############## ###################
#%m code infomation
#%p DEBUG,INFO,WARN,ERROR,FATAL
#%r cost milisecond from start to print
#%d{yyy MMM dd HH:mm:ss , SSS}
#%n enter
#%l line
###################################
################################
# common appenders
################################
#stdout configure
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-all file appender
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-warn file appender
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-error file appender
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-dao file appender
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-web file appender
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-pageNotFound file appender
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
################################
# project appenders
################################
#project-dao file appender
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-web file appender
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-service file appender
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
################################
# open source framework loggers
################################
#spring
log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUG,common-dao
# debug log for ibatis2.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
# debug log for ibatis3
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: Changing the log level to DEBUG will display SQL Hibernate generated
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
#springmvc pageNotFound
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/log4jdbc for details
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
################################
# project loggers
################################
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.类中引用
public class Log4jTest{
private static Logger logger = Logger.getLogger("Log4jTest");//参数一般为类名
public static int chu(int num1,int num2){
try{
return num1/num2;
}catch(Exception ex){
logger.error("操作错误",ex);
//logger.error("操作错误");
return 0;
}
}
}