log4cxx:使用参数配置追加器 [英] log4cxx: configuring appender with arguments
问题描述
log4cxx的配置通过以下方式从后续xml中读取:
log4cxx's config is read from follow-by xml via:
DOMConfigurator::configure("log4cxx.xml");
但是,要在运行时设置文件名,这会产生一个问题,即要读取多个.xml或在运行中(无论在何处,在内存/磁盘中)创建一个.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>
是否可以通过任何方法将一些值传递给配置器以替换为<param file="file" value="%%value%%" />
或在配置和更改后访问属性?
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.这样做的原因:同一程序的多个副本正在写入同一日志,从而使其看起来很奇怪.如何以传统方式使用log4j/log4cxx避免这种情况,而又不发明过多的自行车?
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?
推荐答案
大约2年前这个问题袭击了我;我再也无法访问源代码了,但是或多或少,这就是我所做的:
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:
- 扩展了FileAppender,并在配置文件中使用了它.
- 在
setFile()
方法内,我在文件名后附加了进程名和id(如果不使用滚动文件,则可以在setOption()
方法中完成此操作.
- 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 thesetOption()
method, if you don't use a rolling file.
我们已经有了一种通过内部ID唯一标识我们的流程及其实例的机制;例如,您可以使用PID,也可以添加全局变量,然后在每个应用程序中使用log4cxx将其设置为唯一值,然后在添加程序的setOption
方法中使用它.
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.
另请参阅我的答案:将Log4j CompositeTriggeringPolicy功能与log4CXX一起使用
这篇关于log4cxx:使用参数配置追加器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!