RE: Setting log file name dynamically
We don’t use property files, only the xml config file. I tried defining my appender as but made the call setenv("LOGNAME", "tester.log", 1); in my app and tried sending a log message to this logger. The message disappeared though--no “tester.log” was created. Not sure where the message got logged… From: David Schornack [mailto:[EMAIL PROTECTED] Sent: Monday, June 09, 2008 12:26 PM To: Log4CXX User Subject: Re: Setting log file name dynamically Yes, there is a much easier method, at least this works very well for me. In log4j.properties, define the following ... log4j.rootLogger=info, R log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=../data/${logfile.name} and then in your code you simple set the name as follows ... logger = Logger::getLogger("MyApp"); setenv("logfile.name", "MyApp.log", 1); Very simple! David On Mon, 2008-06-09 at 11:38 -0700, Peter Steele wrote: I am using the following code to set the log file dynamically instead of defining it in the xml file: DOMConfigurator::configure(config); LoggerPtr logger = Logger::getLogger(cat_name); FileAppenderPtr appender = logger->getAppender("FILE"); Pool p; appender->setFile(log_name); appender->activateOptions(p); This works with one small side effect. It seems I have to provide a default setting for the File parameter in the xml config file, otherwise the configure command complains when the app runs. So I tried doing this: but this ended up creating an empty file called TBD before the call to setFile had a chance to run. I have a workaround where I set File to /dev/null instead of TBD and this seems to work. Is there a better/correct way to do this? ]
Re: Setting log file name dynamically
Yes, there is a much easier method, at least this works very well for me. In log4j.properties, define the following ... log4j.rootLogger=info, R log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=../data/${logfile.name} and then in your code you simple set the name as follows ... logger = Logger::getLogger("MyApp"); setenv("logfile.name", "MyApp.log", 1); Very simple! David On Mon, 2008-06-09 at 11:38 -0700, Peter Steele wrote: > I am using the following code to set the log file dynamically instead > of defining it in the xml file: > > > > DOMConfigurator::configure(config); > > LoggerPtr logger = Logger::getLogger(cat_name); > > FileAppenderPtr appender = logger->getAppender("FILE"); > > Pool p; > > appender->setFile(log_name); > > appender->activateOptions(p); > > > > This works with one small side effect. It seems I have to provide a > default setting for the File parameter in the xml config file, > otherwise the configure command complains when the app runs. So I > tried doing this: > > > > > > > > > > > > > > > > > > > > but this ended up creating an empty file called TBD before the call to > setFile had a chance to run. I have a workaround where I set File > to /dev/null instead of TBD and this seems to work. Is there a > better/correct way to do this? > > ] > >
Setting log file name dynamically
I am using the following code to set the log file dynamically instead of defining it in the xml file: DOMConfigurator::configure(config); LoggerPtr logger = Logger::getLogger(cat_name); FileAppenderPtr appender = logger->getAppender("FILE"); Pool p; appender->setFile(log_name); appender->activateOptions(p); This works with one small side effect. It seems I have to provide a default setting for the File parameter in the xml config file, otherwise the configure command complains when the app runs. So I tried doing this: but this ended up creating an empty file called TBD before the call to setFile had a chance to run. I have a workaround where I set File to /dev/null instead of TBD and this seems to work. Is there a better/correct way to do this? ]