Log4cplus setproperty函数使用

Log4cplus setproperty function usage

我在 conf 文件中为我的记录器使用以下配置:

log4cplus.appender.TestLogAppender = log4cplus::TimeBasedRollingFileAppender
log4cplus.appender.TestLogAppender.FilenamePattern = %d{yyyyMMdd}.log
log4cplus.appender.TestLogAppender.MaxHistory = 365
log4cplus.appender.TestLogAppender.Schedule = DAILY
log4cplus.appender.TestLogAppender.RollOnClose = false
log4cplus.appender.TestLogAppender.layout = log4cplus::PatternLayout
log4cplus.appender.TestLogAppender.layout.ConversionPattern = %m%n

在我的代码中,我有以下用于我的记录器的初始化函数,首先,我加载配置文件,然后我希望将 'FilenamePattern' 属性 设置为一个新的值,这样当我 运行 多个应用程序时,每个应用程序都会写入它自己的日志文件:

void InitLogger()
{
  ProperyConfigurator::doConfigure (L"LogConf.conf");
  helpers:Properties prop(L"LogConf.conf");
  props.setPropery(L"log4cplus.appender.TestLogAppender.FilenamePattern" , 
  "Log/AppLogName.log.%d{yyyy-MM-dd}");
}

问题是,当我 运行 即使是一个应用程序时,日志消息也会写入原始配置文件中给定的文件(在 'FilenamePattern' 属性 中)。 'setproperty' 似乎没有设置我给它的新值。

我初始化记录器功能有问题吗? 我是不是用错了setProperty方法?

您显然是在配置系统后更改属性,因此您的更改将被忽略。改为这样做:

helpers:Properties props(L"LogConf.conf");
props.setPropery(L"log4cplus.appender.TestLogAppender.FilenamePattern" , 
  "Log/AppLogName.log.%d{yyyy-MM-dd}");
ProperyConfigurator propConf (props);
propConf.configure();