以跨平台方式记录到临时目录

时间:2022-02-10 12:21:10

I have an application for which log4j logging is configured in a log4j.properties file. Currently, this application runs on UNIX and creates a log file in /tmp. This application needs to run on Windows, and on that platform I would like for it to select the correct temporary directory, which I believe is C:\temp.

我有一个应用程序,在log4j.properties文件中配置log4j日志记录。目前,此应用程序在UNIX上运行并在/ tmp中创建日志文件。这个应用程序需要在Windows上运行,在那个平台上我想让它选择正确的临时目录,我相信它是C:\ temp。

How can I change my log4j.properties file to make this happen? Do I need to switch to using an XML configuration file?

如何更改我的log4j.properties文件以实现此目的?我是否需要切换到使用XML配置文件?

2 个解决方案

#1


18  

I think you would just use ${java.io.tmpdir} in place of a hard-coded path.

我想你只需使用$ {java.io.tmpdir}代替硬编码路径。

#2


0  

As of Log4J v1.2.14, I was able to use this in both a log4j.xml file as well as a log4j.properties file. There was some discussion on the web that variables wouldn't parse in the DOMReader, but they do as of this version of log4j.

从Log4J v1.2.14开始,我能够在log4j.xml文件和log4j.properties文件中使用它。在网上有一些关于变量不会在DOMReader中解析的讨论,但是他们在这个版本的log4j中做了。

<appender name="rolling_file_appender_ourapp" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${user.home}/.mycompany/OurApp.log" />
    <param name="Append" value="false" />
    <param name="MaxFileSize" value="10MB" />
    <param name="MaxBackupIndex" value="3" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d | %-5p | %c | %m | %t | %x %n" />
    </layout>
</appender>

or

log4j.appender.rfile=org.apache.log4j.FileAppender
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.Append=false
log4j.appender.rfile.layout.ConversionPattern=%d [%p] %c %m%n
log4j.appender.rfile.File=${user.home}/.mycompany/OurApp.log

#1


18  

I think you would just use ${java.io.tmpdir} in place of a hard-coded path.

我想你只需使用$ {java.io.tmpdir}代替硬编码路径。

#2


0  

As of Log4J v1.2.14, I was able to use this in both a log4j.xml file as well as a log4j.properties file. There was some discussion on the web that variables wouldn't parse in the DOMReader, but they do as of this version of log4j.

从Log4J v1.2.14开始,我能够在log4j.xml文件和log4j.properties文件中使用它。在网上有一些关于变量不会在DOMReader中解析的讨论,但是他们在这个版本的log4j中做了。

<appender name="rolling_file_appender_ourapp" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${user.home}/.mycompany/OurApp.log" />
    <param name="Append" value="false" />
    <param name="MaxFileSize" value="10MB" />
    <param name="MaxBackupIndex" value="3" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d | %-5p | %c | %m | %t | %x %n" />
    </layout>
</appender>

or

log4j.appender.rfile=org.apache.log4j.FileAppender
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.Append=false
log4j.appender.rfile.layout.ConversionPattern=%d [%p] %c %m%n
log4j.appender.rfile.File=${user.home}/.mycompany/OurApp.log