Relative paths are used in the log4j.properties file.
How can i find the absolute path programatically where logs are stored?
Apache Log4j is a Java-based logging utility originally written by Ceki Gülcü. It is part of the Apache Logging Services, a project of the Apache Software Foundation. Log4j is one of several Java logging frameworks. Apache Log4j.
Navigate into the "META-INF" sub-directory and open the file "MANIFEST. MF" in a text editor. Find the line starting with "Implementation-Version", this is the Log4j version.
Check Log4j Version For that, we need to make use of the “apt list” instruction on the shell along with the name of a library as “liblog4j2-java” as shown in the image below. The output is showing “Listing… Done”, and after that, it is showing the installed version of Log4j2 in our system i.e., version “2.17. 1-0.20.
From: http://www.gunith.com/2010/11/how-to-get-the-file-path-of-a-log4j-log-file/
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 Logger
FileAppender appender = (FileAppender)logger.getAppender("migration");
return new File(appender.getFile());
Note that migrationlog was used to create the logger object in the first line. And migration is used to get the FileAppender which in turn calls getFile() to get the log File object.
I think one way is like this:
since the path is relative to system property "user.dir"
so relative path = ./app.log
becomes {user.dir}/app.log
get user.dir as System.getproperty("user.dir").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With