log4cxx: configuring appender with arguments

巧了我就是萌 提交于 2020-01-04 11:39:22

问题


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?


回答1:


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:

  1. Extended the FileAppender and used it in the config file.
  2. 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



来源:https://stackoverflow.com/questions/5487233/log4cxx-configuring-appender-with-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!