logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Schornack <dschorn...@gtplimited.com>
Subject Re: Setting log file name dynamically
Date Mon, 09 Jun 2008 19:25:45 GMT
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