Java log4j 相对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2975289/
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-08-13 15:07:31  来源:igfitidea点击:

log4j relative file path

javalog4j

提问by Bob

I'd like my web app to log into files with this path: webapp/logs/

我希望我的网络应用程序使用以下路径登录文件:webapp/logs/

I can set the absolute path in the log4j.properties file, but the production environment's directory structure will be different. Is there any way I could do it?

我可以在log4j.properties文件中设置绝对路径,但是生产环境的目录结构会有所不同。有什么办法可以做到吗?

Here is how I do:

这是我的做法:

log4j.appender.f=org.apache.log4j.RollingFileAppender
log4j.appender.f.layout=org.apache.log4j.PatternLayout
log4j.appender.f.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.f.File=log.out
log4j.appender.f.MaxFileSize=100KB

This is printing logs into a file named log.log in my eclipse directory (c://eclipse). I'm using Tomcat 6.

这是将日志打印到我的 eclipse 目录 (c://eclipse) 中名为 log.log 的文件中。我正在使用 Tomcat 6。

采纳答案by matt b

log4j is capable of expanding system properties, so if your production environment sets a property for the directory which you would like to place the log files in, you can reference it from the log4j.properties file.

log4j 能够扩展系统属性,因此如果您的生产环境为您想要放置日志文件的目录设置了一个属性,您可以从 log4j.properties 文件中引用它。

For example, we deploy webapps on Tomcat as well. Tomcat sets up a system property named catalina.basewhich points to the Tomcat base directory. A log4j configuration that looks like this:

例如,我们也在 Tomcat 上部署了 webapp。Tomcat 设置了一个名为的系统属性catalina.base,该属性指向 Tomcat 基目录。如下所示的 log4j 配置:

 log4j.appender.f.File = ${catalina.base}/logs/myapp.log

Will result in the myapp.logfile being stored in the logs directory under the Tomcat install directory.

将导致该myapp.log文件存储在Tomcat安装目录下的logs目录中。

回答by B. Smalley

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
log4j.appender.A1.DatePattern='-'yyyy-MM-dd'.log'

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
log4j.appender.A1.DatePattern='-'yyyy-MM-dd' 。日志'

In this example, your current logfile will be named "myapp.log". At midnight (or when the first log entry occurs on the next day) "myapp.log" will be renamed to "myapp-yyyy-mm-dd.log" (for example,
"myapp-2010-12-21.log") and a new "myapp.log" will be created.

在此示例中,您当前的日志文件将命名为“myapp.log”。在午夜(或第二天出现第一个日志条目时)“myapp.log”将重命名为“myapp-yyyy-mm-dd.log”(例如,
“myapp-2010-12-21.log” ) 并创建一个新的“myapp.log”。

回答by despot

Its best to provide:
log4j.appender.f.File = ./myapp.log

最好提供:
log4j.appender.f.File = ./myapp.log

. represents the current folder (usually the project root folder). This also works on different OSs. In case when you use ${catalina.home} you might not be running a web application through tomcat.

. 代表当前文件夹(通常是项目根文件夹)。这也适用于不同的操作系统。如果您使用 ${catalina.home},您可能不会通过 tomcat 运行 Web 应用程序。

This post helped me go through the issue:
http://www.matjazcerkvenik.si/Site/Java::Log4j_Properties.html(updated link http://www.matjazcerkvenik.si/developer/java-log4j.php)

这篇文章帮助我解决了这个问题:
http: //www.matjazcerkvenik.si/Site/Java:: Log4j_Properties.html(更新链接http://www.matjazcerkvenik.si/developer/java-log4j.php

Cheers!

干杯!