c++ - 如何在 log4cplus 中添加自定义过滤器?

标签 c++ log4cplus

这是 this question 的跟进在条件日志记录上。

我在 log4cplus 测试目录中找到了如何在属性文件中配置过滤器的示例(因此不需要使用 XML) 我已经创建了自己的过滤器,但在使用时遇到了问题。以下是我所做的更改:

在 spi/filter.h 中:

class LOG4CPLUS_EXPORT InstructionNumberFilter : public Filter {
    public:
      // ctors
        InstructionNumberFilter();
        InstructionNumberFilter(const log4cplus::helpers::Properties& p);

        /**
         * Returns {@link #NEUTRAL} is there is no string match.
         */
        virtual FilterResult decide(const InternalLoggingEvent& event) const;

    private:
      // Methods
        LOG4CPLUS_PRIVATE void init();

      // Data
        /** Do we return ACCEPT when a match occurs. Default is <code>true</code>. */
        uint instructionNumberToMatch;

};

在 spi/filter.cxx 中:

InstructionNumberFilter::InstructionNumberFilter()
{
    init();
}

InstructionNumberFilter::InstructionNumberFilter(const helpers::Properties& properties)
{
    init();
    properties.getUInt(instructionNumberToMatch, LOG4CPLUS_TEXT("InstructionNumber"));    
}    

void
InstructionNumberFilter::init()
{
    instructionNumberToMatch = 0;
}    

FilterResult
InstructionNumberFilter::decide(const InternalLoggingEvent& event) const
{
    const uint currentInstructionNumber = 4; // TODO get number from MDC

    if( currentInstructionNumber == instructionNumberToMatch ){
        return ACCEPT;
    }

    return NEUTRAL;
}

在 factory.cxx 中:

LOG4CPLUS_REG_FILTER (reg3, InstructionNumberFilter);

在属性文件中:

# Set up logging to standard output stream.

log4cplus.appender.AP1=log4cplus::ConsoleAppender
log4cplus.appender.AP1.layout=log4cplus::PatternLayout
log4cplus.appender.AP1.layout.ConversionPattern=Rabble %-5p MDC(instructionNumber):%-10X{instructionNumber} [%d{%Q}](%l): %m

log4cplus.appender.AP1.filters.1=log4cplus::spi::InstructionNumberFilter
log4cplus.appender.AP1.filters.1.InstructionNumberToMatch=4
log4cplus.appender.AP1.filters.2=log4cplus::spi::DenyAllFilter

当我运行时出现错误:

log4cplus:ERROR Appender::ctor()- Cannot find FilterFactory: log4cplus::spi::InstructionNumberFilter

我试图将所有更改都基于 StringMatchFilter 的实现。我是否缺少其他需要做的事情才能让我的过滤器被识别?

感谢您的帮助。

最佳答案

该错误是错误定位从 log4cplus 构建的库的结果。

因此,上面的示例在 log4cplus 中实现自定义过滤器的方法。除非有人反对,否则我会把它留在这里,因为网络上很少有 log4cplus 过滤器的例子。

关于c++ - 如何在 log4cplus 中添加自定义过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921728/

相关文章:

c++ - 在屏幕尺寸变化时保持几何体完整

c++ - 重新定义日志记录宏

c++ - 尝试获取不存在的节点时如何不出现任何错误

c++ - 基于 log4j 的记录器 : log4cpp vs log4cplus vs log4cxx

c++ - Firebreath 不会使用 Xcode 5.0.2 进行编译

c++ - Log4cplus RollingFileAppender 是否异步

c++ - 为什么 QTcpSocket 不写?我需要刷新它才能发送我的数据

指针 vector 中的 C++ 垃圾值

C++ 日志库设置

c++ - 如果我使用日志记录库,SDL 不会显示其窗口