3

I would like to read in a configuration file to log4cxx, but during runtime, I would like to modify the appenders/loggers. Can I do this?

Example log4cxx file:

log4j.rootLogger=all, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.console.threshold=info

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %c [%p] %l %m%n
log4j.appender.file.filename=logfile.txt
log4j.appender.file.datePattern='.'yyyy-MM-dd

Example source:

PropertyConfigurator::configure("./LoggingConfig.txt");

auto rootlogger = Logger::getRootLogger();
DailyRollingFileAppenderPtr fileappender = rootlogger->getAppender(LOG4CXX_STR("file"));

fileappender->setFile(LOG4CXX_STR("thisisthenewfile.txt"));

--I've noticed that the output still writes to the original location in my configuration file.

Is it not possibly to configure using code (runtime) as well as a file?

Jason
  • 33
  • 1
  • 5

1 Answers1

0

Yes it is possible to modify appender or logger through your code although you have log4crc file. I have done this in log4c in the following way.
log4c_appender_t *myappender = log4c_appender_get("man.appender"); to change prefix or log dir you need to create object of rolling file udata in the following way:
rollingfile_udata_t *rfu = rollingfile_make_udata();
then set path
rollingfile_udata_set_logdir(rfu,"my/sample/path");
or set prefix too
rollingfile_udata_set_files_prefix(rfu,"myPrefix");
then set rolling file with appender
log4c_appender_set_udata(myappender,rfu);
and also you may want to set the appender with category
log4c_category_set_appender(mycat,myappender);

Sandeep
  • 106
  • 1
  • 4