Using Log4j CompositeTriggeringPolicy feature with

2019-08-07 03:29发布

I would like to combine both rolling time and rolling size in an appender, it seems there is no composite rolling in log4cxx, am I right ?

标签: c++ log4cxx
1条回答
聊天终结者
2楼-- · 2019-08-07 04:29

No, there isn't. In fact, there is no such combined policy implemented in log4j, either, so it was not transferred to log4cxx.

I once had a task of writing such mixed-policy rolling file appender (in log4j, though). I did this by overriding FileAppender, esp. the method

// log4j
void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize);
// log4cxx
void setFile(const LogString& filename, bool append1, bool bufferedIO1, size_t bufferSize1, Pool& p);

to continue logging to the appropriate log chunk after process restart, and

// log4j
void subAppend(LoggingEvent event);
// log4cxx
virtual void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p); 

to test after every log entry whether the condition for rolling over is true.

A truly powerful feature of implementing your own appender in log4j/cxx is that you can define its properties in log4j.properties and have them set by the library. In Java you get it for free, thanks to reflection; in log4cxx you only need to write your

void setOption(const LogString& option, const LogString& value);

method, where you iterate through option strings and perform necessary initialization actions.

Hope that helps and comes in the right time.

查看更多
登录 后发表回答