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 Curt Arnold <ca...@apache.org> on 2005/08/05 06:32:50 UTC

Re: File Location Substitution.


On Aug 4, 2005, at 10:55 PM, macdev wrote:

> I know I can set the location of the logfile used by Log4CXX using  
> the config files. I do this in the snippet I have included below.  
> But how do I tailor the location at runtime? For example on a unix  
> system how can make sure the logs go to a specific folder beneath  
> the users home folder.
>
> logdir=/Users/devguy/library/Application Support/logs
>
> log4j.appender.PriParser=org.apache.log4j.FileAppender
> log4j.appender.PriParser.File=${logdir}/parser_internal.log
>
> Thanks.
> p.s. I bought the manual hoping it would help me, but what I am  
> looking for eludes me.

log4cxx mimics the user.home, user.name, user.dir, java.io.tmpdir  
properties that work with log4j.  So you could do:

> log4j.appender.PriParser.File=${user.home}/parser_internal.log




Re: File Location Substitution.

Posted by Vijai Kalyan <vi...@gmail.com>.
Does this work for XML configuration files as well? Or do I need to
specify the variables in a different form?

thanks,

-vijai.

On 8/5/05, Curt Arnold <ca...@apache.org> wrote:
> 
> 
> On Aug 4, 2005, at 10:55 PM, macdev wrote:
> 
> > I know I can set the location of the logfile used by Log4CXX using
> > the config files. I do this in the snippet I have included below.
> > But how do I tailor the location at runtime? For example on a unix
> > system how can make sure the logs go to a specific folder beneath
> > the users home folder.
> >
> > logdir=/Users/devguy/library/Application Support/logs
> >
> > log4j.appender.PriParser=org.apache.log4j.FileAppender
> > log4j.appender.PriParser.File=${logdir}/parser_internal.log
> >
> > Thanks.
> > p.s. I bought the manual hoping it would help me, but what I am
> > looking for eludes me.
> 
> log4cxx mimics the user.home, user.name, user.dir, java.io.tmpdir
> properties that work with log4j.  So you could do:
> 
> > log4j.appender.PriParser.File=${user.home}/parser_internal.log
> 
> 
> 
>

Re: File Location Substitution.

Posted by Micha <th...@yahoo.com>.
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




RE : File Location Substitution.

Posted by Alexandre Gacon <al...@masagroup.net>.
Instead of calling the file with the pid in the name, couldn't you use a
unique rolling file and output the thread id in the result, using a pattern
appender for example ?

Alexandre

-----Message d'origine-----
De : maarten [mailto:maartenb@dns.be] 
Envoyé : vendredi 5 août 2005 09:17
À : Log4CXX User
Objet : Re: File Location Substitution.


Are there any other properties that I can use as placeholders in my 
log4cxx.properties file.
Can I use environment variables, that will be expanded at runtime ? What
about the process id ?

What I do now is define a RollingFile appender in my properties file and 
do my own hard-coded initialization :

  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();
  }

Thanks,
Maarten

Curt Arnold wrote:

>
>
> On Aug 4, 2005, at 10:55 PM, macdev wrote:
>
>> I know I can set the location of the logfile used by Log4CXX using
>> the config files. I do this in the snippet I have included below.  
>> But how do I tailor the location at runtime? For example on a unix  
>> system how can make sure the logs go to a specific folder beneath  
>> the users home folder.
>>
>> logdir=/Users/devguy/library/Application Support/logs
>>
>> log4j.appender.PriParser=org.apache.log4j.FileAppender
>> log4j.appender.PriParser.File=${logdir}/parser_internal.log
>>
>> Thanks.
>> p.s. I bought the manual hoping it would help me, but what I am
>> looking for eludes me.
>
>
> log4cxx mimics the user.home, user.name, user.dir, java.io.tmpdir  
> properties that work with log4j.  So you could do:
>
>> log4j.appender.PriParser.File=${user.home}/parser_internal.log
>
>
>
>
>



Re: File Location Substitution.

Posted by maarten <ma...@dns.be>.
Are there any other properties that I can use as placeholders in my 
log4cxx.properties file.
Can I use environment variables, that will be expanded at runtime ?
What about the process id ?

What I do now is define a RollingFile appender in my properties file and 
do my own hard-coded initialization :

  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();
  }

Thanks,
Maarten

Curt Arnold wrote:

>
>
> On Aug 4, 2005, at 10:55 PM, macdev wrote:
>
>> I know I can set the location of the logfile used by Log4CXX using  
>> the config files. I do this in the snippet I have included below.  
>> But how do I tailor the location at runtime? For example on a unix  
>> system how can make sure the logs go to a specific folder beneath  
>> the users home folder.
>>
>> logdir=/Users/devguy/library/Application Support/logs
>>
>> log4j.appender.PriParser=org.apache.log4j.FileAppender
>> log4j.appender.PriParser.File=${logdir}/parser_internal.log
>>
>> Thanks.
>> p.s. I bought the manual hoping it would help me, but what I am  
>> looking for eludes me.
>
>
> log4cxx mimics the user.home, user.name, user.dir, java.io.tmpdir  
> properties that work with log4j.  So you could do:
>
>> log4j.appender.PriParser.File=${user.home}/parser_internal.log
>
>
>
>
>