log4j相对文件路径 [英] log4j relative file path
问题描述
我希望我的网络应用程序使用以下路径登录文件:
webapp / logs /
I'd like my web app to log into files with this path: webapp/logs/
我可以在log4j中设置绝对路径.properties文件,但生产环境的目录结构会有所不同。
我有什么方法可以做到吗?
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.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
这是在我的eclipse目录(c:// eclipse)中将日志打印到名为log.log的文件中。
我正在使用Tomcat 6.
This is printing logs into a file named log.log in my eclipse directory (c://eclipse). I'm using Tomcat 6.
推荐答案
log4j能够扩展系统属性,因此如果你的生产环境设置了您希望将日志文件放在其中的目录的属性,您可以从log4j.properties文件中引用它。
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.
例如,我们在Tomcat上部署webapps同样。 Tomcat设置一个名为 catalina.base
的系统属性,该属性指向Tomcat基目录。 log4j配置如下所示:
For example, we deploy webapps on Tomcat as well. Tomcat sets up a system property named catalina.base
which points to the Tomcat base directory. A log4j configuration that looks like this:
log4j.appender.f.File = ${catalina.base}/logs/myapp.log
将导致 myapp.log
文件存储在Tomcat安装目录下的logs目录中。
Will result in the myapp.log
file being stored in the logs directory under the Tomcat install directory.
这篇关于log4j相对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!