开发者

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

Relative paths are used in the log4j.properties file.

开发者_开发问答

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


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").
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜