java log4j 属性文件:如何配置?

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

log4j properties file: how to configure?

javalogginglog4j

提问by EugeneP

My procedure for using log4j is this:

我使用 log4j 的过程是这样的:

  1. put a .propertiesfile somewhere in a project folder
  2. in an initialization method, that runs only once, invoke PropertyConfigurator.configure("path_to_file")
  3. in every method we need to use logger we define a static logger variable and simply invoke getLogger(class)
  1. .properties文件放在项目文件夹中的某处
  2. 在只运行一次的初始化方法中,调用PropertyConfigurator.configure("path_to_file")
  3. 在我们需要使用记录器的每个方法中,我们定义一个静态记录器变量并简单地调用getLogger(class)

Is this correct? What happens if the initialization module is not defined? Where can I put the "log4j.properties" file such that I don't have to invoke propertyconfigurator.configure at all? If that's not possible, is it OK to invoke PropertyConfigurator.configure("path_to_file")in every method that uses a logger?

这个对吗?如果未定义初始化模块会发生什么?我可以把“log4j.properties”文件放在哪里,这样我就不必调用 propertyconfigurator.configure 了?如果这是不可能的,是否可以在使用记录器的每个方法中调用PropertyConfigurator.configure("path_to_file")

回答by Péter T?r?k

If you put it somewhere in the classpath, Log4J will automatically load it. A typical place for it is the source (or resource) root directory. That way it can get copied into the root directory of your jar too (if you create one from your class files).

如果你把它放在类路径中的某个地方,Log4J 会自动加载它。它的典型位置是源(或资源)根目录。这样它也可以被复制到你的 jar 的根目录中(如果你从你的类文件中创建一个)。

The exact details for this depend on what build system you use. E.g. in Maven the convention is to put it in src/main/resources.

确切的细节取决于您使用的构建系统。例如在 Maven 中,约定是将它放在src/main/resources.

回答by Stephen C

The default initialization procedure for log4j is documented in this sectionof the log4j tutorial. This explains in detail the steps that log4j takes to locate the logging configuration.

log4j 教程的这一部分记录了 log4j的默认初始化过程。这详细解释了 log4j 用于定位日志记录配置的步骤。

The simplest way to configure log4j is to put a the properties file somewhere that allows it to be found by the class loader using the name "/log4j.properties". There are other approaches that you can use as well that involve setting system properties, loading the properties file programmatically, or even instantiating the Loggers, Appenders and so on via the log4j APIs. (Not that the latter is a good idea ...)

配置 log4j 的最简单方法是将属性文件放在允许类加载器使用名称“/log4j.properties”找到的某处。您还可以使用其他方法,包括设置系统属性、以编程方式加载属性文件,甚至通过 log4j API 实例化 Logger、Appender 等。(并不是说后者是个好主意......)

(Putting the log4j properties file in "src/main/resources" in a Maven project is just one of a number of possible ways to get the file into your application's classpath root.)

(将 log4j 属性文件放在 Maven 项目的“src/main/resources”中只是将文件放入应用程序的类路径根目录的多种可能方法之一。)