Is it possible to write a custom appender for log4cxx and have it configurable via a properties file (like the built-in appenders)? I'd prefer doing this without having to rebuild log4cxx (e.g. by deriving/extending an existing appender), if possible.
Can you point me toward an example?
I am adding this answer as the the links in the most rated answer appear to be from an older version. I have created a custom appender using version 1.2.0 following what was done in their SysLogAppender. At a high level you will need to do these steps:
void close()
andvoid append(const spi::InternalLoggingEvent& event)
Here is an example class with code for class with the namespace yournamespace::ExampleCustomAppender:
Then the implementation of the methods:
and lastly an example of how it may be used in the configuration file:
I did this code by cutting up some other code so it has not been compiled per se in this exact format. Let me know of any issues and I will correct it.
You can inherit from AppenderSkeleton or WriterAppender and get the same underlying behaviors without having to rebuilt log4cxx.
http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.h?view=markup http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.cpp?view=markup
brlcad's suggestion is correct - at my company we've used this method for our own custom appenders.
You can in fact already configure these from a configuration file - the macros like DECLARE_LOG4CXX_OBJECT(CustomAppenderClassName) in the class definition set up the log4cxx environment. You can refer to your appender type by your "CustomAppenderClassName". A configfile (old-style) showing a standard and custom appender, using the standard layouts for the custom appender: