class FileUtils{
public static String getSelfPath(){
String path = new FileUtils().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if(System.getProperty("os.name").contains("dows"))
{
path = path.substring(1,path.length());
}
if(path.contains("jar"))
{
path = path.substring(0,path.lastIndexOf("."));
return (path.substring(0,path.lastIndexOf("/")));
}
String s = (path.replace("target/classes/", ""));
if(s.endsWith("/") || s.endsWith("\\")){
s = s.substring(0, s.length() - 1);
}
return s;
}
}
System.setProperty("log_home", FileUtils.getSelfPath() + "/logs");
PropertyConfigurator.configure(FileUtils.getSelfPath() + "/config/log4j.properties");
把log4j.propertis建在项目的根目录confing目录下面
#服务器配置======
#log4j.rootLogger=info,rollfile,errorfile
#服务器配置 end======
#本地配置======
log4j.rootLogger=info,stdout,rollfile,errorfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n
#本地配置 end======
########################
# Rolling File
########################
log4j.appender.rollfile=org.apache.log4j.FileAppender
log4j.appender.rollfile=org.apache.log4j.RollingFileAppender
log4j.appender.rollfile.File=${log_home}/logout.log
log4j.appender.rollfile.Append=true
log4j.appender.rollfile.Threshold=INFO
log4j.appender.rollfile.MaxFileSize=100MB
log4j.appender.rollfile.MaxBackupIndex=20
log4j.appender.rollfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n
############################
#DailyRollingFileAppender
############################
log4j.appender.errorfile=org.apache.log4j.FileAppender
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile.File=${log_home}/error.log
log4j.appender.errorfile.Append=true
log4j.appender.errorfile.Threshold=ERROR
log4j.appender.errorfile.MaxFileSize=100MB
log4j.appender.errorfile.MaxBackupIndex=20
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=[TXJH] %d{yyyy-MM-dd HH:mm:ss} - %m%n
####################
#Logger
#####################
log4j.logger.org.springframework=WARN
log4j.logger.java.sql=INFO
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.java.sql.Connection=WARN
log4j.logger.org.apache.ibatis=debug
log4j.logger.com.utils.Utils=INFO