我怎样才能使 log4j fileAppender 正确转义 ${java.io.tmpdir} 的内容

发布于 2024-07-27 00:33:25 字数 760 浏览 1 评论 0原文

我有一个像这样配置的 log4j RollingFileAppender

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="MaxBackupIndex" value="10" />
    <param name="File" value="${java.io.tmpdir}\\myLogFile.log" />
</appender>

我的问题是 java.io.tmpdir 映射到 C:\Temp 并且 log4j 没有转义反斜杠。 当我启动该过程时,我看到以下错误,

java.io.FileNotFoundException: c:   emp\myLogFile.log (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unk

我无法修改所有用户计算机上的环境变量,并且我不想对日志路径进行硬编码。

我可以编写自己的 RollingFileAppender 扩展来解决这个问题,但我希望有一个预先存在的方法可以解决这个问题。

我最好的选择是什么?

I have a log4j RollingFileAppender configured like this

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="MaxBackupIndex" value="10" />
    <param name="File" value="${java.io.tmpdir}\\myLogFile.log" />
</appender>

my problem is that java.io.tmpdir is mapped to C:\Temp and log4j does not escape the backslash.
When I start the process I see the following error

java.io.FileNotFoundException: c:   emp\myLogFile.log (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unk

it is not possible for me to modify the Environment Variables on all my users' machines and I do not want to hard-code the log path.

I could write my own extension to RollingFileAppender to fix this, but I was hoping that there would be a pre-existing way around this.

What are my best options ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-08-03 00:33:25

你试过这个吗?

<param name="File" value="${java.io.tmpdir}/myLogFile.log" />

您不需要在 XML 文件中转义反斜杠,并且 log4j 应该完全能够将该路径转换为您需要的任何特定于平台的路径(Windows 上的反斜杠)。

Have you tried this?

<param name="File" value="${java.io.tmpdir}/myLogFile.log" />

You shouldn't need to escape backslashes within the XML file, and log4j should be perfectly capable of transforming that path into whatever platform-specific path (backslashes on Windows) you need.

醉殇 2024-08-03 00:33:25

如果您使用属性文件而不是 XML 文件,这种情况可能不会发生。 查看 log4j 源代码,可以看出 DOMConfigurator 正在进行导致错误的 TAB 替换。 PropertyConfigurator 不执行此替换。 或许可以尝试一下?

您还可以将 java.io.tmpdir 更改为 C:/Temp 并在适当的情况下使用 / 代替 \。 但这听起来对你来说不是一个选择吗?

It looks like this might not happen if you use a properties file instead of an XML file. Looking at the log4j source code, it seams that DOMConfigurator is making the TAB substitution that is causing the error. PropertyConfigurator doesn't perform this substitution. Give it a shot maybe?

You could also change java.io.tmpdir to C:/Temp and use / instead of \ where appropriate. That doesn't sound like an option for you though?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文