通过扩展FileAppender类的方式,定制属于自己的相对路径起始位置:
1.扩展属于自己的FileAppender类,并重载setFile方法
public class RelativePathDailyRollingFileAppender extends DailyRollingFileAppender{
/**
* 获取eclipse的运行时工作空间根路径
*/
public static String RELATIVE_PATH = Platform.getLocation().toString() + "/";
@Override
public void setFile(String file) {
super.setFile(RELATIVE_PATH+file);
}
}
2.改变log4j中的配置项,把Appender实现类换成我们自己的实现类
#log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info=cn.com.agree.abop.ide.logging.appender.RelativePathDailyRollingFileAppender
通过修改log4j配置文件,使生成的日志根据日志级别保存在多个文件中
# 按照级别把日志输出到不同的文件
log4j.rootLogger=info,stdout,info,debug,error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.logger.info=info
#log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info=cn.com.agree.abop.ide.logging.appender.RelativePathDailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = INFO
log4j.appender.info.append=true
log4j.appender.info.File=.metadata/info.log
log4j.logger.error=error
log4j.appender.error=cn.com.agree.abop.ide.logging.appender.RelativePathDailyRollingFileAppender
#log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.File=.metadata/error.log
如下是我使用的log4j配置文件的全文:
log4j.rootLogger=WARN,CONSOLE,DATABASE,FILE
log4j.addivity.org.apache=true
# 输出到控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=GBK
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# 按照级别把日志输出到不同的文件
log4j.rootLogger=info,stdout,info,debug,error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.logger.info=info
#log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info=cn.com.agree.abop.ide.logging.appender.RelativePathDailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = INFO
log4j.appender.info.append=true
log4j.appender.info.File=.metadata/info.log
log4j.logger.error=error
log4j.appender.error=cn.com.agree.abop.ide.logging.appender.RelativePathDailyRollingFileAppender
#log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.File=.metadata/error.log
#log4j.logger.debug=debug
#log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.debug.layout=org.apache.log4j.PatternLayout
#log4j.appender.debug.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
#log4j.appender.debug.datePattern='.'yyyy-MM-dd
#log4j.appender.debug.Threshold = DEBUG
#log4j.appender.debug.append=true
#log4j.appender.debug.File=.metadata/debug.log