如何在log4cplus中添加自定义过滤器? [英] How do I add a custom filter in log4cplus?
问题描述
这是此问题的后续追踪条件日志。
This is a followup to this question on conditional logging.
我在log4cplus测试目录中找到了如何在属性文件中配置过滤器的示例(所以使用XML是不必要的)我创建了自己的过滤器,但我有麻烦使用它。以下是我所做的更改:
I found examples in the log4cplus test directory of how to configure filters in the properties file (so using the XML is unnecessary) I have created my own filter but I am having trouble using it. Here are the changes I made:
在spi / filter.h中:
In 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中:
In 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:
In 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的实现。我缺少一些我需要做的事情,让我的过滤器被识别?
I tried to base all my changes on the implementation of the StringMatchFilter. Am I missing something else I need to do to get my filter to be recognized?
感谢您的帮助。
推荐答案
错误是由于从log4cplus构建的库错误定位的结果。
The error was a result of mislocating the libraries built from log4cplus.
因此,上述示例是在log4cplus中实现自定义过滤器的方法。除非有任何对象,我会离开这里,因为在网络上有很少的log4cplus过滤器的例子。
So, the example above is the way to implement a custom filter in log4cplus. Unless anyone objects I will leave this here as there are very few examples of log4cplus filters on the web.
这篇关于如何在log4cplus中添加自定义过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!