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 drkmkzs <dr...@gmail.com> on 2012/02/22 16:55:40 UTC

Concurrent access

Hi,

I'm currently using Log4Cxx in a C++ application, with a log file property.
Now i have to launch several instances of this program, which means several
processes will used the same log property file, and therefore the same log
file...

I guess it is not handled by log4cxx ? I know it's thread safe, but for
concurrent access in the same main application (like thread or fork), not
between different applications ?

And i imagine a better approach would be to configure log4cxx within the
code (instead property file) in order to change log file name for each
processess ?

Thanks in advance,
sorry for my english
Jean

Re: Concurrent access

Posted by drkmkzs <dr...@gmail.com>.
Hey !

This is a really helpfull answer, that's exactly what I wanted, because
like you I don't really like to put log configuration within the code :)

I'm looking forward to use it ;)
I'll try later to determine Log4cxx behaviour with 2 applic, just to know,
it's still an intersting question...

Thanks a lot Thorsten !

2012/2/23 Thorsten Schöning <ts...@am-soft.de>

> Guten Tag drkmkzs,
> am Mittwoch, 22. Februar 2012 um 16:55 schrieben Sie:
>
> > And i imagine a better approach would be to configure log4cxx within the
> > code (instead property file) in order to change log file name for each
> > processess ?
>
> Not necessarily. I don't know if log4cxx properly handles concurrent
> access to the same file on a per process basis, but I personally
> dislike configuring log4cxx in code and in your case it wouldn't be
> necessary, depending on what your really try to achieve with your log
> files. If it's just about to separate the log files of more than one
> instance of your program, one approach could be to provide a unique
> environment variable in your program on per instance basis before
> configuring log4cxx with its property or xml file, because log4cxx is
> able to use environment variable in the configuration at least in
> paths. I use that to distinct two calls of the same program by
> different callers where the callers provide an id of who they are and
> that id is set as an environment variable and used in the log
> configuration as part of the path to the log file. This ID can be
> whatever you like, process id, GUID, something from the caller, it all
> depends on what you do with the logs.
>
> The syntax to get something from the environment is ${something}, like
> in the following example:
>
> <appender       name="Dbxml.monthSpecificLogFile"
>                class="org.apache.log4j.rolling.RollingFileAppender">
>        <rollingPolicy
>  class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
>                <param  name="FileNamePattern"
>
>  value="..\..\..\Datenserver\Logdateien\dbxml\dbxml\${mandant}\%d{yyyy-MM}.log"
>                />
>        </rollingPolicy>
>        <layout class="org.apache.log4j.PatternLayout">
>                <param  name="ConversionPattern"
>                        value="%d{yyyy-MM-dd HH:mm:ss,SSS} %p> %c.%M: %m%n"
>                />
>        </layout>
>        <param  name="append"
>                value="true"
>        />
> </appender>
>
> Mit freundlichen Grüßen,
>
> Thorsten Schöning
>
> --
> Thorsten Schöning       E-Mail:Thorsten.Schoening@AM-SoFT.de
> AM-SoFT IT-Systeme      http://www.AM-SoFT.de/
>
> Telefon.............030-2 1001-310
> Fax...............05151-  9468- 88
> Mobil..............0178-8 9468- 04
>
> AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
> AG Hanover HRB 207 694 - Geschäftsführer: Andreas Muchow
>
>

Re: Concurrent access

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag drkmkzs,
am Mittwoch, 22. Februar 2012 um 16:55 schrieben Sie:

> And i imagine a better approach would be to configure log4cxx within the
> code (instead property file) in order to change log file name for each
> processess ?

Not necessarily. I don't know if log4cxx properly handles concurrent
access to the same file on a per process basis, but I personally
dislike configuring log4cxx in code and in your case it wouldn't be
necessary, depending on what your really try to achieve with your log
files. If it's just about to separate the log files of more than one
instance of your program, one approach could be to provide a unique
environment variable in your program on per instance basis before
configuring log4cxx with its property or xml file, because log4cxx is
able to use environment variable in the configuration at least in
paths. I use that to distinct two calls of the same program by
different callers where the callers provide an id of who they are and
that id is set as an environment variable and used in the log
configuration as part of the path to the log file. This ID can be
whatever you like, process id, GUID, something from the caller, it all
depends on what you do with the logs.

The syntax to get something from the environment is ${something}, like
in the following example:

<appender       name="Dbxml.monthSpecificLogFile"
                class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy  class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
                <param  name="FileNamePattern"
                        value="..\..\..\Datenserver\Logdateien\dbxml\dbxml\${mandant}\%d{yyyy-MM}.log"
                />
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
                <param  name="ConversionPattern"
                        value="%d{yyyy-MM-dd HH:mm:ss,SSS} %p> %c.%M: %m%n"
                />
        </layout>
        <param  name="append"
                value="true"
        />
</appender>

Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning       E-Mail:Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme      http://www.AM-SoFT.de/

Telefon.............030-2 1001-310
Fax...............05151-  9468- 88
Mobil..............0178-8 9468- 04

AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hanover HRB 207 694 - Geschäftsführer: Andreas Muchow