Log4cxx custom appender

前端 未结 3 1997
梦谈多话
梦谈多话 2021-01-05 16:30

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

3条回答
  •  走了就别回头了
    2021-01-05 16:56

    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:

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

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

    #include 
    
    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 
    #include 
    
    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 lastly an example of how it may be used in the 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 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.

提交回复
热议问题