Log4cxx 自定义附加程序

发布于 2024-07-20 20:21:53 字数 130 浏览 5 评论 0原文

是否可以为 log4cxx 编写自定义附加程序并通过属性文件对其进行配置(如内置附加程序)? 如果可能的话,我更愿意在不必重建 log4cxx 的情况下执行此操作(例如通过派生/扩展现有的附加程序)。

你能给我举个例子吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你鎻心 2024-07-27 20:21:53

brlcad 的建议是正确的 - 在我的公司,我们已将这种方法用于我们自己的自定义附加程序。

事实上,您已经可以从配置文件中配置这些 - 类定义中的 DECLARE_LOG4CXX_OBJECT(CustomAppenderClassName) 等宏设置了 log4cxx 环境。 您可以通过“CustomAppenderClassName”来引用您的附加程序类型。
显示标准和自定义附加程序的配置文件(旧式),使用自定义附加程序的标准布局:

# 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

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:

# 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
一抹微笑 2024-07-27 20:21:53

我添加这个答案是因为评分最高的答案中的链接似乎来自旧版本。 我使用 版本 1.2.0 遵循 SysLogAppender 中所做的操作。 在较高级别上,您将需要执行以下步骤:

  • 创建一个您自己的类,该类继承其 Appender 类。
  • 实现纯虚函数 void close()void append(const spi::InternalLoggingEvent& event)
  • 向 AppenderFactoryRegistry 注册您的附加程序类。

这是一个示例类,其中包含名称空间为 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);
    };
}

然后是方法的实现:

#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();
        }
    }
}

最后是如何在配置文件中使用它的示例:

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 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 <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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文