1、配置log4j的配置文件
#log4j.properties配置
log4j.rootLogger=ALL, A1
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=C:/log4j/TEST.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
2、java中重新设置输出文件
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
public class SetLogFileName {
public SetLogFileName(String filename){
DailyRollingFileAppender appender = (DailyRollingFileAppender)Logger.getRootLogger().getAppender("A1");
appender.setFile( "c:/log4j/"+filename+".log");//动态地修改这个文件名
appender.activateOptions();
}
}
3、调用log4j输出文件
import org.apache.log4j.Logger;
public class OutPutLog {
static Logger logger = Logger.getLogger(OutPutLog.class);
/**
* @param args
*/
public static void main(String[] args) {
SetLogFileName D = new SetLogFileName("trust");
// TODO Auto-generated method stub
logger.info("PIG");
}
}
---------------------
原文:https://blog.csdn.net/zhaofmyan/article/details/6875923