Where can I programmatically find where the log4j log files are stored?

Relative paths are used in the log4j.properties file.

How can I find the absolute path programmatically where the logs are stored?

+6
java log4j
source share
2 answers

From: http://www.gunith.com/2010/11/how-to-get-the-file-path-of-a-log4j-log-file/

Suppose the log4j.properties file is shown 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 this case, your Java code should be as follows:

 Logger logger = Logger.getLogger("migrationlog"); //Defining the Logger FileAppender appender = (FileAppender)logger.getAppender("migration"); return new File(appender.getFile()); 

Note that miglog was used to create the log object in the first line. And migration is used to get the FileAppender file, which in turn calls getFile () to get the log file.

+9
source share

I think one way:

since the path refers to the system property "user.dir"

therefore the relative path = ./app.log becomes {user.dir}/app.log

 get user.dir as System.getproperty("user.dir"). 
+3
source share

All Articles