Custom Log4cxx counter

Is it possible to write a custom appender for log4cxx and configure it through a properties file (for example, embedded applications)? I would prefer to do this without having to rebuild log4cxx (for example, by retrieving / extending an existing application), if possible.

Can you give me an example?

+4
source share
3 answers

The brlcad offer is correct - in my company we used this method for our own custom applications.

In fact, you can already configure them from the configuration file — macros, such as DECLARE_LOG4CXX_OBJECT (CustomAppenderClassName) in the class definition, set the log4cxx environment. You can reference your application type with your "CustomAppenderClassName". Configuration file (old style) showing the standard and user appender using standard layouts for the user application:

# Set "realtime" logger level to DEBUG and create two appenders - one to be # a standard rolling file appender, the other custom. log4j.logger.realtime=ERROR, StandardAppender, CustomAppender # StandardAppenderis set to be a RollingFileAppender log4j.appender.StandardAppender=org.apache.log4j.RollingFileAppender log4j.appender.StandardAppender.File=example.log log4j.appender.StandardAppender.MaxFileSize=100KB # Keep one backup file log4j.appender.StandardAppender.MaxBackupIndex=1 # StandardAppender uses PatternLayout. log4j.appender.StandardAppender.layout=org.apache.log4j.PatternLayout log4j.appender.StandardAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # CustomAppender uses PatternLayout too log4j.appender.CustomAppender=CustomAppenderClassName log4j.appender.CustomAppender.layout=org.apache.log4j.PatternLayout log4j.appender.CustomAppender.layout.ConversionPattern=%m 
+3
source

I am adding this answer as the links in the highest rated answer look from an older version. I created a custom appender using version 1.2.0 , following what was done in their SysLogAppender. At a high level, you need to do the following:

  • Create your own class that inherits their Appender class.
  • Implement pure virtual functions void close() and void append(const spi::InternalLoggingEvent& event)
  • Register your appender class with AppenderFactoryRegistry.

Here is an example class with code for a class with the namespace yournamespace :: ExampleCustomAppender:

 #include <log4cplus/appender.h> namespace yournamespace { class ExampleCustomAppender : public Appender { public: ExampleCustomAppender(const log4cplus::helpers::Properties & properties); // Dtor virtual ~ExampleCustomAppender(); // Methods virtual void close(); /** Register the appender with the factory registry. */ static void registerAppender(); protected: virtual void append(const spi::InternalLoggingEvent& event); }; } 

Then the implementation of the methods:

 #include <log4cplus/spi/factory.h> #include <sstream> namespace yournamespace { ExampleCustomAppender::ExampleCustomAppender(const helpers::Properties & properties) : Appender(properties) { // if you want to use properties to configure your object do so here } ExampleCustomAppender::~ExampleCustomAppender() { } void ExampleCustomAppender::registerAppender() { log4cplus::spi::AppenderFactoryRegistry & reg = log4cplus::spi::getAppenderFactoryRegistry(); LOG4CPLUS_REG_PRODUCT(reg, "log4cplus::", ExampleCustomAppender, yournamespace::, log4cplus::spi::AppenderFactory); } void ExampleCustomAppender::close() { // do anything you need to close the appender closed = true; } // This method does not need to be locked since it is called by // doAppend() which performs the locking void LoggingCallbackAppender::append(const spi::InternalLoggingEvent& event) { if (!closed) { std::stringstream logOutput; layout->formatAndAppend(logOutput, event); std::string outputString = logOutput.str(); } } } 

and finally, an example of how it can be used in a configuration file:

 log4cplus.rootLogger=INFO, STDOUT, ROLLING, EXAMPLE // other definitions for STDOUT and ROLLING here and then ... log4cplus.appender.EXAMPLE=log4cplus::ExampleCustomAppender log4cplus.appender.EXAMPLE.layout=log4cplus::PatternLayout log4cplus.appender.EXAMPLE.layout.ConversionPattern=%d{%m/%d %H:%M:%S} [%t] %-5p %c{2} - %m%n 

I made this code by rendering another code, so it was not compiled as such in this exact format. Notify me of any problems and I will fix it.

+1
source

All Articles