You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Micha <th...@yahoo.com> on 2005/09/15 06:51:37 UTC

Re: File Location Substitution.

maarten <maartenb <at> dns.be> writes:
> 
>   Appender* appender = Logger::getRootLogger()->getAppender ("RollingFile");
>   FileAppender* fa = dynamic_cast<FileAppender*>(appender);
>   if (fa != 0) {
>      string fullpath = "/var/log/example_" + getEnvVar("REMOTE_IP") + 
> "_" + getDatetime() + "_" + getPID() + ".log";
>      fa->setFile ( fullpath );
>      fa->activateOptions();
>   }
> 


While the above will work it is ugly and requires all linked libraries to be
compiled with RTTI support which might not be reasonable.

I've struggled with this for some time (I need a new log for each network port)
and came up with a more elegant solution using variable substitution: create a
configuration file (i.e, logging port) with a substitution for the name or the
part of the name you want to change, for example:

file: logging.cfg
-----------------
log4j.rootLogger=DEBUG, logfile
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=d:\logname_${LOGPORT}
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{%d %b %Y %H:%M:%S.%Q} %-5p %c
- %m%n
log4j.logger.Monitor=DEBUG

Then, in your code and BEFORE you load the configuration, put a value into the
the substitution variable using _putenv :

code
----
_putenv("LOGPORT=5100");
PropertyConfigurator::configure("logging.conf");

This will only work if you know the new filename before you load the
configuration but maybe there's a way to reload it later ? (I'm new to log4cxx)

Hope this helps,
Micha