是否可以以编程方式配置 log4cpp?
我想通过提供以下配置文件来配置它。
appender root basic console
priority root DEBUG
你是说 log4cpp 来自 http://sourceforge.net/projects/log4cpp/吗?
简短的回答是肯定的。
以下是一些示例代码,您可以根据其文档找出更多详细信息:
我的代码用于在运行时配置文件的基于文件的 log4cpp 实例。
"优先级"是"调试",...
这里的"名称"是"根"
"layout" 可以是 "%d{%Y-%m-%d %H:%M:%S}%c %x: %m%n"
// Create RollingFileAppender
log4cpp::Appender *rfileAppender = new log4cpp::RollingFileAppender(std::string(name),
std::string(fileName),
maxFileSize,
maxBackupIndex,
append,
mode);
if (rfileAppender != NULL)
{
// Create PatternLayout
log4cpp::Layout *layout = new log4cpp::PatternLayout();
if (layout != NULL)
{
try
{
// Set up Pattern
((log4cpp::PatternLayout *)layout)->setConversionPattern(std::string(layoutPattern));
// Bind Layout to RollingFileAppender
rfileAppender->setLayout(layout);
category.setAdditivity(additivity);
try
{
category.setPriority(log4cpp::Priority::getPriorityValue(std::string(priority)));
}
catch(std::invalid_argument &ia)
{
std::cerr << "Invalid Priority: " << priority << std::endl;
category.setPriority(log4cpp::Priority::INFO);
}
// Bind RollingFileAppender to Category
category.addAppender(rfileAppender);
return 0;
}
catch(log4cpp::ConfigureFailure &cf)
{
std::cerr << cf.what() << std::endl;
return 1;
}
}
else
{
std::cerr << "Cannot initialize PatternLayout" << std::endl;
return 1;
}
}