Log to different file with log4cxx

Novalis picture Novalis · Jan 9, 2012 · Viewed 14.1k times · Source

I want to log to different files in my code.

How can i do that in Log4cxx with xml configuration or programatically in code...

  • Suppose that I have 1.k,k+1,..n components.
  • They run in the same application
  • I want component k log to Logger-k, k+1 component log to Logger-k+1 at the same time

Update:

Logger.addAppender() approach:

log4cxx::helpers::Pool p; 

std::string paramAppender = "appxNormalAppender";
std::string paramFileName = "\\Logs\\MyLog.txt";

LOG4CXX_DECODE_CHAR(logAppender, paramAppender );
LOG4CXX_DECODE_CHAR(logFileName, paramFileName );


FileAppenderPtr fileAppender = 
logger->getLoggerRepository()->getRootLogger()->getAppender(logAppender);

if(fileAppender!= NULL)
{


    fileAppender->setFile(logFileName);

    fileAppender->activateOptions(p);

}

This does not work because

Suppose that i set FileName to Logger-k for k component, it logs to Logger-k, then i set file name to Logger-k+1 for compoent k+1, then both component k, and k+1 log the same loggerk+1 file. It seems last file name overrides or effects all others...

And

All compoenent compenent 1, ...compoeent k, componentk+1,.... component n are in the same application...

Answer

diverscuba23 picture diverscuba23 · Jan 9, 2012

Create Each of the Loggers as normal, and then for each logger add a FileAppender set to the desired file via the Logger.addAppender() method.