一直以来,我就有这样的想法:log4j的log文件可以这样定义:与A主题(A主题可能包含若干个类)相关的日志记录到A.log中,与B主题相关的日志记录到B.log中,……。这种要求下,那样在各个类中以类名获得log实例的方法是不可行的,例如:Logger log = Logger.getLogger(Class.getName())。
我一直在调试log4j.properties,今天终于成功了。
1、类路径中加入commons-logging.jar,log4j-1.2.8.jar
2、内路径中加入commons-logging.properties,log4j.properties文件,其内容分别如下:
commons-logging.properties
**************************************************************************************
#log4j logging
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
#jdk1.4 logging
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#LogKit
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger
#SimpleLog
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
**************************************************************************************
log4j.properties
**************************************************************************************
#### AAA、BBB logger
log4j.rootCategory=stdout, R1, R2
#定义rootLogger的这句不能缺少!否则记录器AAA,BBB会有重复的记录
log4j.rootLogger=INFO, stdout
#与AAA相关的内容将会记录到AAAlog.log中,与BBB相关的内容将会记录到BBBlog.log中
log4j.logger.AAA=stdout, R1
log4j.logger.BBB=stdout, R2
#### appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=info
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#### appender writes to a file
log4j.appender.R1=org.apache.log4j.RollingFileAppender
log4j.appender.R1.Threshold=info
log4j.appender.R1.File=AAAlog.log
log4j.appender.R1.MaxFileSize=200KB
log4j.appender.R1.MaxBackupIndex=5
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.R1.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#### appender writes to a file
log4j.appender.R2=org.apache.log4j.RollingFileAppender
log4j.appender.R2.Threshold=info
log4j.appender.R2.File=BBBlog.log
log4j.appender.R2.MaxFileSize=200KB
log4j.appender.R2.MaxBackupIndex=5
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.R2.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#程序生成记录器只需:Log logA = LogFactory.getLog("AAA");,以logA记录的信息将写入AAAlog.log文件中
我一直在调试log4j.properties,今天终于成功了。
1、类路径中加入commons-logging.jar,log4j-1.2.8.jar
2、内路径中加入commons-logging.properties,log4j.properties文件,其内容分别如下:
commons-logging.properties
**************************************************************************************
#log4j logging
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
#jdk1.4 logging
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#LogKit
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger
#SimpleLog
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
**************************************************************************************
log4j.properties
**************************************************************************************
#### AAA、BBB logger
log4j.rootCategory=stdout, R1, R2
#定义rootLogger的这句不能缺少!否则记录器AAA,BBB会有重复的记录
log4j.rootLogger=INFO, stdout
#与AAA相关的内容将会记录到AAAlog.log中,与BBB相关的内容将会记录到BBBlog.log中
log4j.logger.AAA=stdout, R1
log4j.logger.BBB=stdout, R2
#### appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=info
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#### appender writes to a file
log4j.appender.R1=org.apache.log4j.RollingFileAppender
log4j.appender.R1.Threshold=info
log4j.appender.R1.File=AAAlog.log
log4j.appender.R1.MaxFileSize=200KB
log4j.appender.R1.MaxBackupIndex=5
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.R1.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#### appender writes to a file
log4j.appender.R2=org.apache.log4j.RollingFileAppender
log4j.appender.R2.Threshold=info
log4j.appender.R2.File=BBBlog.log
log4j.appender.R2.MaxFileSize=200KB
log4j.appender.R2.MaxBackupIndex=5
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}] - %m%n
#log4j.appender.R2.layout.ConversionPattern=%-5p[%d{yyyy-MM-dd HH:mm:ss SSSS}](%F:%L) - %m%n
#程序生成记录器只需:Log logA = LogFactory.getLog("AAA");,以logA记录的信息将写入AAAlog.log文件中