user.dir 系统属性在 commons-daemon procrun & 下如何解释?日志4j?
我已经使用 tomcat 5.5.33 附带的 procrun 版本安装了 java 批处理进程:
Commons Daemon Service Runner version 1.0.5.0/Win32 (Jan 5 2011)
Copyright (c) 2000-2011 The Apache Software Foundation.
在安装中,我指定(以及其他 JVM 选项):
--JvmOptions="-Duser.dir=C:\LOCAL\serverapps"
我的 log4j.properties 配置包括:
log4j.appender.InfoLogFile.File=../logs/info.log
但是,正在写入 info.log 文件to:
C:\WINDOWS\logs
我在许多不同的点检查了 user.dir
的值,它始终是 C:\LOCAL\serverapps
。
但是,log4j 的行为就像 user.dir=C:\Windows\System32
(或 C:\Windows
的其他子目录)。
从 log4j 源代码 (1.2.16) 中可以看出,FileAppender
仅处理 java.io.FileOutputStream
和 File 类,它们声称使路径相对于user.dir
位置。
我已经解决了这个问题,但我很好奇:还有其他人遇到过这种类型的行为吗?如果是这样,到底发生了什么?
I have installed a java batch process using the version of procrun that ships with tomcat 5.5.33:
Commons Daemon Service Runner version 1.0.5.0/Win32 (Jan 5 2011)
Copyright (c) 2000-2011 The Apache Software Foundation.
In the installation, I specify (among other JVM options):
--JvmOptions="-Duser.dir=C:\LOCAL\serverapps"
My log4j.properties configuration includes:
log4j.appender.InfoLogFile.File=../logs/info.log
However, the info.log file is being written to:
C:\WINDOWS\logs
I've checked the value of user.dir
at many different points and it's always C:\LOCAL\serverapps
.
But, log4j is behaving as if user.dir=C:\Windows\System32
(or some other subir of C:\Windows
).
From what I can tell from the log4j source (1.2.16), the FileAppender
deals only with the java.io.FileOutputStream
and File classes which claim to make paths relative from the user.dir
location.
I've worked around the issue, but I am curious: has anyone else has encountered this type of behaviour? If so, what's really going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当给定相对路径时,FileAppender 将在当前工作目录(而不是用户主目录)中创建一个文件。
您需要在文件名中传递 ${user.dir}。
源代码:
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/FileAppender.java?view=markup
编辑:请参阅下面的评论进行更正 - user.dir != user.home
https://bugs.java.com/bugdatabase/view_bug?bug_id=4117557
FileAppender, when given a relative path, creates a file withing the current working directory - not the user home directory.
You need to pass the ${user.dir} within the filename.
SRC:
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/FileAppender.java?view=markup
EDIT: see comment below for correction - user.dir != user.home
https://bugs.java.com/bugdatabase/view_bug?bug_id=4117557
我在
lo4j.properties
中使用了${user.dir}
并且它有效。你尝试过吗?I have used
${user.dir}
in thelo4j.properties
and it has worked. Have you tried?PhilW 的评论指出了原始问题的正确答案。也就是说,Oracle/Sun 声明了一个问题 https://bugs.java.com/bugdatabase /view_bug?bug_id=4117557 当通过命令行设置
user.dir
时。这就是为什么在写出日志文件时无法正确理解相对路径的原因。通过使用绝对路径(甚至以
${user.dir}
为前缀——此时可以信任——即使 JVM 在内部得到错误的值),就像 Phil、Amir 和我一样建议您完全避免这个问题。PhilW's comment points to the correct answer to the original question. That is, Oracle/Sun declares an issue https://bugs.java.com/bugdatabase/view_bug?bug_id=4117557 when
user.dir
is set via the command line. That is the reason why the relative path is not properly understood when logging files are written out.By using a an absolute path (even prefixing with
${user.dir}
-- which can be trusted at that point - even if the JVM gets the value wrong internally) as Phil, Amir and I all suggest, you avoid the issue altogether.