I am using log4j to log information. I have used a log4j.xml file for creating log files. I have given the absolute path for each log file as a param tag value.
E.g.:
<appender name="FA" class="org.apache.log4j.DailyRollingFileAppender">
<param name="DatePattern" value="'_'yyyyMMdd"/>
<param name="File" value="D:/logFiles/GPreprocessor.log"/>
<layout class="com.dnb.genericpreprocessor.common.log.AppXMLLayout"/>
</appender>
I do not want to write "GPreprocessor.log" directly. Actually, that file name is dynamic, based on my project's name. For example, if I run the program ABC.java, logging should go to D:/logFiles/ABC.log, but if I run XYZ.java, logging should go to D:/logFiles/XYZ.log. The file's location will always remain the same: D:/logFiles/. How can I change the log file's name dynamically?
It's much easier to do the following:
In log4j.xml define variable as ${variable}:
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="${logfilename}.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
Then make sure you set the system property when you start your JVM such as:
java -Dlogfilename=my_fancy_filename example.Application
That will create a dynamic log file name: my_fancy_filename.log
Alternatively, you can set the system property in code so long as you do it before you create a logger (this is useful if you want your PID in your logs for instance). Such as:
System.setProperty("logfilename", "a_cool_logname");
Once that is set you can go ahead and get your loggers as normal and they will log to the dynamic file (be careful of those static Loggers that create loggers before your main method executes).
Below is my code for using Log4J for dynamically generate filename. It changes its name according to input file name and current date-time. (So helpful in case you run same file multiple times.)
public class LogClass {
private static Logger log = Logger.getLogger(LogClass.class);
private static boolean initializationFlag = false;
private static String fileName;
private static void intializeLogger(){
log.setLevel(Level.DEBUG);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
RollingFileAppender appender = new RollingFileAppender();
appender.setAppend(true);
appender.setMaxFileSize("1MB");
appender.setMaxBackupIndex(1);
appender.setFile(fileName + "_" + dateFormat.format(date) + ".log");
PatternLayout layOut = new PatternLayout();
layOut.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
appender.setLayout(layOut);
log.addAppender(appender);
}
public static Logger getLogger(){
if(initializationFlag == false){
intializeLogger();
initializationFlag = true;
return LogClass.log;
}
else{
return LogClass.log;
}
}
public static void setFileName(String fileName){
LogClass.fileName = fileName;
}
}
Now whenever you want to use logger in your program, Just write these two lines :
LogClass.setFileName(yourFileName);
LogClass.getLogger().debug("hello!!");
Happy Coding.
It makes more sense to extend FileAppender with your own class, in which you override setOptions() method. Then in your log4j.properties you configure root to log to yourpackage.yourFileAppender, which is much cleaner.
In your class containing main method set the name of your class to some system property. In following example I used log_dir as property name.
class ABC{
public static void main(String s[]){
System.setProperty("log_dir", ABC.class.getSimpleName());
}
}
And in your log4j.xml file use log_dir property in File param's value attribute
<appender name="FA" class="org.apache.log4j.DailyRollingFileAppender">
<param name="DatePattern" value="'_'yyyyMMdd"/>
<param name="File" value="D:/logFiles/${log_dir}"/>
<layout class="com.dnb.genericpreprocessor.common.log.AppXMLLayout"/>
</appender>
Works like a charm
来源:https://stackoverflow.com/questions/2810926/how-to-give-dynamic-file-name-in-the-appender-in-log4j-xml