c++11log4cpluslog4cpp

configure log4cplus to log to custom file at runtime


I am using configuration file to initialize log4cplus module. We use the below command in configuration file to specify the log file name to be generated.

log4cplus.appender.log1.File=TestFile.log

I want this TestFile.log to be generated at run time. I used setProperty function to change log file name at run time but didn't get the desired result.

In code -

log4cplus::helpers::Properties props; props.setProperty(LOG4CPLUS_TEXT("File"), LOG4CPLUS_TEXT("TestFile.log"));

In configuration file -

log4cplus.appender.afile2.File=${File}

But this will generate log file as "${File}" instead of "TestFile.log".


Solution

  • Try the following line in your command.

    log4cplus::helpers::Properties props("TextConfiguration.txt");
        props.setProperty(LOG4CPLUS_TEXT("log4cplus.appender.afile2.File"), LOG4CPLUS_TEXT("TestFile.log"));
    PropertyConfigurator conf(props);
    conf.configure();
    

    No need to change in configuration file.