java 我可以在哪里以编程方式找到 log4j 日志文件的存储位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3217296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:50:42  来源:igfitidea点击:

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

javalog4j

提问by JavaRocky

Relative paths are used in the log4j.properties file.

log4j.properties 文件中使用了相对路径。

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

如何以编程方式找到存储日志的绝对路径?

回答by Basil Musa

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

来自: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.properties 文件如下,

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,

在这种情况下,您的 Java 代码应如下所示,

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

Note that migrationlogwas used to create the logger object in the first line. And migrationis used to get the FileAppender which in turn calls getFile() to get the log File object.

请注意,migrationlog用于在第一行中创建记录器对象。并且迁移用于获取 FileAppender,后者又调用 getFile() 来获取日志文件对象。

回答by Inv3r53

I think one way is like this:

我认为一种方法是这样的:

since the path is relative to system property "user.dir"

因为路径是相对于系统属性“user.dir”

so relative path = ./app.logbecomes {user.dir}/app.log

所以相对路径 =./app.log变成 {user.dir}/app.log

get user.dir as System.getproperty("user.dir").