gpt4 book ai didi

java - 配置 Log4j 属性路径的最佳实践

转载 作者:搜寻专家 更新时间:2023-10-31 19:41:45 25 4
gpt4 key购买 nike

我在我的应用程序中使用 Log4j。 log4j.properties 放置在类路径中的 Jar 中。此配置文件正在使用并且大部分时间都可以正常工作。

但有时,生成的日志语句与配置文件不一致。

我的理解是,这是因为属性文件是从类路径中获取的。无论第一个属性文件是什么,都会被 log4j 获取和使用。因此,有时我们得到的日志语句与属性文件中配置的不同。

如果上述原因成立,那么我认为,我们需要通过以下方式之一具体指定配置文件:

  1. 使用系统属性指定:-Dlog4j.configuration=log4j.properties
  2. 正在初始化 Log4jInit servlet。

请建议哪种方法更好。

最佳答案

在我的应用程序中,我使用 ${user.home} 目录来存储 log4j.properties 并使用我们正在读取的 ant。它将独立于平台。您也可以通过内置类路径传递一个,但从 ${user.home} 读取是更好的方法,您或任何非技术人员都可以轻松访问它。

你也可以用

Dynamic Log File Location

Many people complain that Log4j forces you to hard-code the location where your logs will be kept. Actually, it is possible to dynamically choose the log-file location, especially if you use the ${log.dir} property substitution technique above. Here's how:

String dynamicLog = // log directory somehow chosen...
Properties p = new Properties( Config.ETC + "/log4j.properties" );
p.put( "log.dir", dynamicLog ); // overwrite "log.dir"
PropertyConfigurator.configure( p );

另见

关于java - 配置 Log4j 属性路径的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com