第一步:首先建立一个WEB工程,去log4j官网下log4j的JAR包导入到工程的lib目录下
第二步:在src目录下建一个log4j.properties 文件,文件命名可以由自己,只是记加载时候和这里名字一致就行;
log4j.properties 里边的内容如下:
set log levels
log4j.rootLogger = debug,stdout,E
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L – %m%n
log4j.appender.E= org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =${catalina.home}/logs/log_
log4j.appender.E.DatePattern=yyyy-MM-dd’.log’
log4j.appender.E.Threshold =DEBUG
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss}[%c] [%t:%r] – [%p] %m%n
可以单指定日志输出到某个文件:
log4j.logger.pay-log=Info,pay-log
log4j.appender.pay-log=org.apache.log4j.DailyRollingFileAppender
log4j.appender.pay-log.File=D:/logs/pay-log.log
log4j.appender.pay-log.DatePattern=’.’yyyy-MM-dd
log4j.appender.pay-log.Threshold =DEBUG
log4j.appender.pay-log.layout=org.apache.log4j.PatternLayout
log4j.appender.pay-log.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss}[%c] [%t:%r] – [%p] %m%n