How to get the file path of a Log4J log file in Java level?
Assume the log4j.properties file is as below,
log4j.logger.migrationlog = INFO, migration
log4j.appender.migration = org.apache.log4j.RollingFileAppender
log4j.appender.migration.File = C:/work/log/migration.log
log4j.appender.migration.MaxFileSize=20MB
log4j.appender.migration.MaxBackupIndex=1
log4j.appender.migration.layout = org.apache.log4j.PatternLayout
log4j.appender.migration.layout.conversionPattern = %d %-5p %c - %m%n
In such case, your Java code should be as follows,
Logger logger = Logger.getLogger("migrationlog"); //Defining the LoggerFileAppender
appender = (FileAppender)logger.getAppender("migration");
return new File(appender.getFile());
Note that its not migrationlog
which was used when making the logger object is used when retrieving the file name, but migration
Migrated from gunith.com
Categories:
Java
Written on November 20, 2010