问题描述
log4cxx的配置是从后续XML读取的:
DOMConfigurator::configure("log4cxx.xml");
但是,希望在运行时设置文件名,这会产生一个问题,即具有多个.xml用于读取的问题,要么即时(在磁盘上/在磁盘上 - 无论何处)创建一个.
<appender name="appxNormalAppender" class="org.apache.log4j.FileAppender"> <param name="file" value="appxLogFile.log" /> <param name="append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%t:%x] %C{2} (%F:%L) - %m%n" /> </layout> </appender>
有什么方法可以通过配置器一些值代替<param file="file" value="%%value%%" />或在配置和更改之后访问属性?
P.S.这样做的原因是:同一程序的多个副本正在写入相同的日志,使其看起来很差.如何以传统方式避免使用log4j/log4cxx,而不会发明太多自行车?
推荐答案
这个问题大约在2年前打了我.我不再可以访问源代码了,但是这或多或少是我所做的:
- 扩展了Fileappender并在配置文件中使用了它.
- 在setFile()中,我将进程名称和ID附加到文件名(如果您不使用滚动文件,则可以在setOption()方法中进行.
我们已经有一种机制,可以通过内部ID唯一地识别我们的过程及其实例.例如,您可以使用PID,也可以添加全局变量并在每个应用程序中使用log4cxx将其设置为您的唯一值,然后在您的appender的setOption方法中使用它.
另请参阅我的答案:使用Log4J CompoSiteTrign feplasterign log4cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx一个>
问题描述
log4cxx's config is read from follow-by xml via:
DOMConfigurator::configure("log4cxx.xml");
But, want to have filename set at runtime, and this creates a problem of either having multiple .xmls for reading, or creating one on the fly (in memory/at disk -- no matter where).
<appender name="appxNormalAppender" class="org.apache.log4j.FileAppender"> <param name="file" value="appxLogFile.log" /> <param name="append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%t:%x] %C{2} (%F:%L) - %m%n" /> </layout> </appender>
Is there any way to pass configurator some values to substitute into <param file="file" value="%%value%%" /> or to access the property after configuring and change it?
P.S. The reason for doing this: multiple copies of the same program are writing to same log making it look ..strange. How to avoid this with log4j/log4cxx in traditional way without inventing too much bicycles?
推荐答案
This problem hit me about 2 years ago; I don't have access to source code anymore, but here's what I did, more or less:
- Extended the FileAppender and used it in the config file.
- Inside the setFile() method I appended process name and id to the file name (you can do it in the setOption() method, if you don't use a rolling file.
We already had a mechanism for uniquely identifying our processes and their instances by our internal id; you could use a PID, for example, or add a global variable and set it in every application using log4cxx to your unique value, then using it in the setOption method of your appender.
See also my answer: Using Log4j CompositeTriggeringPolicy feature with log4CXX