是否可以为 log4cxx 编写自定义附加程序并通过属性文件(如内置附加程序)对其进行配置?如果可能的话,我宁愿这样做而不必重建 log4cxx(例如,通过派生/扩展现有的 appender)。
你能给我举个例子吗?
您可以从 AppenderSkeleton 或 WriterAppender 继承并获得相同的底层行为,而无需重新构建 log4cxx。
http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src/test/cpp/vectorappender.h?view=markup http://svn.apache.org/viewvc/incubator/log4cxx/trunk/src /test/cpp/vectorappender.cpp?view=markup
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
我正在添加此答案,因为评分最高的答案中的链接似乎来自旧版本。在他们的 SysLogAppender 中完成的操作之后,我使用1.2.0 版创建了一个自定义附加程序。在高层次上,您需要执行以下步骤:
void close()
和void append(const spi::InternalLoggingEvent& event)
这是一个示例类,其中包含命名空间 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
我通过剪切一些其他代码来完成此代码,因此它本身并没有以这种确切的格式编译。让我知道任何问题,我会纠正它。