类1
package service;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test3 {
public static void main(String[] args) {
PropertyConfigurator.configure("E:/work/eclipseTest/test3/src/log4j.properties");//加载.properties文件
Logger logger = Logger.getLogger(Test3.class);//在指定类下建立logger
logger.debug("Test3-debug");
logger.info("Test3-info");
}
}
类2
package service;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test4 {
public static void main(String[] args) {
PropertyConfigurator.configure("E:/work/eclipseTest/test3/src/log4j.properties");//加载.properties文件
Logger logger = Logger.getLogger(Test4.class);//在指定类下建立logger
logger.debug("Test4-debug");
logger.info("Test4-info");
}
}
log4j.properties 配置
log4j.rootLogger=Test,all,System
#service.Test4 为包名.类名 INFO为显示的日志等级 有ERROR、WARN、INFO、DEBUG四个等级
log4j.logger.service.Test4=INFO,Test4
log4j.logger.service.Test3=INFO,Test3
#输出到控制台
log4j.appender.System=org.apache.log4j.ConsoleAppender
log4j.appender.System.Target = System.out
log4j.appender.System.layout=org.apache.log4j.PatternLayout
log4j.appender.System.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%t] %l %-5p - %m%n
#输出all.log文件中
log4j.appender.all=org.apache.log4j.RollingFileAppender
log4j.appender.all.File=F:/img/logs/all.log
log4j.appender.all.MaxFileSize=100MB
log4j.appender.all.MaxBackupIndex=30
log4j.appender.all.Append = true
log4j.appender.all.threshold = INFO
log4j.appender.all.layout=org.apache.log4j.PatternLayout
log4j.appender.all.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%t] %c %-5p - %m%n
#输出Test4.log 文件中
log4j.appender.Test4=org.apache.log4j.RollingFileAppender
log4j.appender.Test4.File=F:/img/logs/Test4.log
log4j.appender.Test4.MaxFileSize=100MB
log4j.appender.Test4.MaxBackupIndex=30
log4j.appender.Test4.Append = true
log4j.appender.Test4.threshold = INFO
log4j.appender.Test4.layout=org.apache.log4j.PatternLayout
log4j.appender.Test4.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%t] %c %-5p - %m%n
#输出Test3.log 文件中
log4j.appender.Test3=org.apache.log4j.RollingFileAppender
log4j.appender.Test3.File=F:/img/logs/Test3.log
log4j.appender.Test3.MaxFileSize=100MB
log4j.appender.Test3.MaxBackupIndex=30
log4j.appender.Test3.Append = true
log4j.appender.Test3.threshold = INFO
log4j.appender.Test3.layout=org.apache.log4j.PatternLayout
log4j.appender.Test3.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%t] %c %-5p - %m%n