logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eberhard, Markus (external)" <Markus.Eberhard.Exter...@eads.com>
Subject Adapt an existing FileAppender
Date Thu, 07 Aug 2008 14:15:49 GMT
Hey *,

I have found the following two lines of code to adapt an existing
FileAppender:

 

Appender* appender = Logger::getRootLogger()->getAppender
("RollingFile");
FileAppender* fa = dynamic_cast<FileAppender*>(appender);

 

But when I try to implement this functionylity in my method I get always
an dynamic_cast exception, here is my code snippet:

 

std::string errorLogFile = "test.log";

const log4cxx::LogString rootLoggerFileAppenderName =
LOG4CXX_STR("RootLoggerFileAppender"); 

...

std::wstring logFilePathName(errorLogFile.begin(), errorLogFile.end());

log4cxx::Appender* rootFileAppender =
log4cxx::Logger::getRootLogger()->getAppender(rootLoggerFileAppenderName
);

if(rootFileAppender == 0 || rootFileAppender == NULL)

{

      log4cxx::LogString logStringFormat = LOG4CXX_STR("[%-5p] - %m%n");

      log4cxx::LayoutPtr layout(new
log4cxx::PatternLayout(logStringFormat));

      rootFileAppender = new log4cxx::FileAppender(layout,
logFilePathName, true);

      rootFileAppender->setName(LOG4CXX_STR("RollingFile"));

      log4cxx::Logger::getRootLogger()->addAppender(rootFileAppender);}

 

/* the following dynamic_cast throws an exception */

try

{

 
dynamic_cast<log4cxx::FileAppender*>(rootFileAppender)->setFile(logFileP
athName);

}

catch(...)

{

 
log4cxx::Logger::getRootLogger()->removeAppender(rootLoggerFileAppenderN
ame);

      log4cxx::BasicConfigurator::configure();

      LOG4CXX_ERROR(m_logger, "FileAppender couldn't be configured for
root logger, invalid log file specified: " << errorLogFile);

      LOG4CXX_ERROR(m_logger, "A ConsoleAppender was added to the root
logger instead of the FileAppender.");

      return 1;

}

 

Where is my bug?

 

Thanks for any respons

Markus


Mime
View raw message