Java 日志文件的 log4j 路径

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

log4j path for logs file

javaxmlpathlog4j

提问by Wojtek

I am using log4j for my logs. My application has a simple XML configuration file and I need to have in my config file the path for the log file. At the moment, it's another XML configuration file for log4j that contains this:

我正在为我的日志使用 log4j。我的应用程序有一个简单的 XML 配置文件,我需要在我的配置文件中有日志文件的路径。目前,它是 log4j 的另一个 XML 配置文件,其中包含以下内容:

<log4j:configuration>
    <appender name="file" class="org.apache.log4j.FileAppender">
    <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
        <param name="file" value="log.out" /> 
        (..........)

(log.outis the default log which is in the project's directory.) How can I move this configuration into my application's configuration file?

log.out是项目目录中的默认日志。)如何将此配置移动到应用程序的配置文件中?

回答by Aaron Digulla

There are basically three options:

基本上有三种选择:

  1. During the build, create a config for log4j which contains an absolute path for the file parameter. Least flexible.

  2. Use a System property. While this gives you some flexibility, this causes problems when you run in a container (J2EE server) and you have several applications which all use log4j.

  1. 在构建期间,为 log4j 创建一个配置,其中包含文件参数的绝对路径。最不灵活。

  2. 使用系统属性。虽然这为您提供了一些灵活性,但是当您在容器(J2EE 服务器)中运行并且您有几个都使用 log4j 的应用程序时,这会导致问题。

The second option comes in two flavors:

第二种选择有两种风格:

  1. You can specify the path for the log file using ${logFile}in the XML and use -DlogFile=on the command line to specify the path.

  2. You can keep the XML config in a different place and tell log4j to load it when it starts using -Dlog4j.configuration=/absolute/path/to/log4j.xml

  1. 您可以${logFile}在 XML 中指定日志文件的路径,并-DlogFile=在命令行中使用来指定路径。

  2. 您可以将 XML 配置保留在不同的位置,并告诉 log4j 在开始使用时加载它 -Dlog4j.configuration=/absolute/path/to/log4j.xml

Related articles:

相关文章: