Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can i programatically find where the log4j log files are stored?

Tags:

java

log4j

Relative paths are used in the log4j.properties file.

How can i find the absolute path programatically where logs are stored?

like image 874
JavaRocky Avatar asked Jul 10 '10 00:07

JavaRocky


People also ask

What is Log4j logging library?

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.

How do I view Log4j in a project?

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.

How do I know if Log4j is installed?

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.


2 Answers

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.

like image 114
Basil Musa Avatar answered Oct 04 '22 13:10

Basil Musa


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").
like image 45
Inv3r53 Avatar answered Oct 03 '22 13:10

Inv3r53