【发布时间】:2014-04-23 10:50:45
【问题描述】:
我想在我的 log4j.xml 文件中使用 .properties 文件中的值。
实际上想在 log4j.xml 文件中设置参数的值;
就像 Spring 中有一些 PropertyPlaceholderConfigurer
我可以在 log4j.xml 中使用什么??
任何形式的指导或建议都是值得赞赏的。
谢谢!
【问题讨论】:
标签: xml logging properties log4j
我想在我的 log4j.xml 文件中使用 .properties 文件中的值。
实际上想在 log4j.xml 文件中设置参数的值;
就像 Spring 中有一些 PropertyPlaceholderConfigurer
我可以在 log4j.xml 中使用什么??
任何形式的指导或建议都是值得赞赏的。
谢谢!
【问题讨论】:
标签: xml logging properties log4j
我知道这个问题很老,但希望偶然发现这个问题的人会发现以下有用。 Log4j 支持属性替换,因此您可以使用存储在 log4j XML 配置文件中的属性或 ini 文件中的值。请参阅 Apache 的 documentation 中的前缀部分。
我使用 .ini 文件执行了属性替换。您也应该能够轻松地使用 .properties 文件执行相同的操作。以下是使用 properties/ini 值动态设置日志文件路径的方法:
//for an .ini file using org.ini4j.Wini
ini = new Wini(new File(args[0]));
System.setProperty("log_dir", ini.get("yourSubHeader", "yourPropertyName"));
//for a .properties file
prop.load(input); //using a FileInputStream
System.setProperty("log_dir", prop.getProperty("yourPropertyName"));
设置系统属性后,您可以使用 sys 前缀通过以下方式在 XML 配置文件中检索此值:
<Properties>
<Property name="log-path" >${sys:log_dir}</Property>
<!-- other properties here -->
</Properties>
希望这对任何想知道同样问题的人有所帮助。
【讨论】: