logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Steele" <pste...@maxiscale.com>
Subject RE: Setting log file name dynamically
Date Mon, 09 Jun 2008 20:23:30 GMT
We don’t use property files, only the xml config file. I tried defining my appender as

 

  <appender name="FILE" class="org.apache.log4j.FileAppender">

    <param name="Append" value="true" />

    <param name="File"   value="${LOGNAME}" />

    <layout class="org.apache.log4j.PatternLayout">

      <param name="ConversionPattern" value="%d %-5p [%t] [%c{1}] %m%n"/>

    </layout>

  </appender>

 

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:dschornack@gtplimited.com] 
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:

 

  <appender name="FILE" class="org.apache.log4j.FileAppender">

    <param name="Append" value="true" />

    <param name="File"   value="TBD" />

    <layout class="org.apache.log4j.PatternLayout">

      <param name="ConversionPattern" value="%d %-5p [%t] [%c{1}] %m%n"/>

    </layout>

  </appender>

 

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?

]



	

 

Mime
View raw message