log4j2: registering custom TriggeringPolicy

谁说胖子不能爱 提交于 2019-12-04 19:19:08

While debugging, it appeared that Routes appenders (defined by the $${sd:type} in my case) are not known during log4j2.xml parsing. Thus, their creation/initialization is delayed in time to the moment when the first message for the destination $${sd:type} arrives. Thus, the original subject of the post could be answered as "custom policy is registered, however not always instantly (or during the .xml parse time)".

The original problem, however, was to force .log files to roll-over at the end of the time period (an hour in my case). To address it, I have implemented following algorithm:

  1. wrote a thin wrapper around TimeBasedTriggeringPolicy - FTimeBasedTriggeringPolicy that registers itself on instantiation at LogRotateThread
  2. wrote a simple LogRotateThread that queries registered FTimeBasedTriggeringPolicy once every few minutes and makes them rotate .log if needed
  3. added a StructuredDataFilter to the Routes appender, so that they discard specific messages (with id=SKIP in my case)
  4. provide an empty message at the system start-up to all known $${sd:type}, which:
    a. instantiate Route appender and cause FTimeBasedTriggeringPolicy to register itself in the LogRotateThread
    b. are discarded by the StructuredDataFilter

Solution is published under Apache 2.0 license, and is available at github

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