Thanks Thomas, this seems to work. I prefer that the number of logger would be 
dynamicaly known at time-execution (depending on the passed parameters) but allowing a 
maximum and defining each one it's a good solution to make it works. Also thanks to 
Scott, who was right to say that I only need to read the configuration file one time!

Thanks people,

Jaume

-----Original Message-----
From: ext Thomas Tuft Muller [mailto:[EMAIL PROTECTED]]
Sent: 22. April 2002 14:33
To: Log4J Users List
Subject: RE: Different logger objects


Jaume,

I'm not exactly sure what you want to obtain, but the following snippet
might help you:

public class Foo {

        private static int counter = 1;

        // instance variable!!
        private Logger logger;

        public Foo() {
                synchronized( Foo.class ) {
                        logger = Logger.getLogger( Foo.class.getName() + "." + ( 
counter++ ) );
                }
        }

}

The configuration of log4j should be kept separate from the actual users of
Log4j,
e.g. in a startup class.

An XML config file could look like this (excerpt):

        <root>
                <Priority value="debug" />
                <appender-ref ref="ROOT" />
        </root>

        <category name="Foo">
                <Priority value="debug"/>
                <appender-ref ref="FOO_ALL"/>
        </category>

        <category name="Foo.1">
                <Priority value="debug"/>
                <appender-ref ref="FOO_1"/>
        </category>

        <category name="Foo.2">
                <Priority value="debug"/>
                <appender-ref ref="FOO_2"/>
        </category>

        <appender name="FOO_ALL"
                        class="org.apache.log4j.FileAppender">
                <param name="File" value="foo-all.log" />
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern"
                        value="%d{ABSOLUTE} [%-5p] [%-8t] - %m%n" />
                </layout>
        </appender>

        <appender name="FOO_1"
                        class="org.apache.log4j.FileAppender">
                <param name="File" value="foo-1.log" />
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern"
                        value="%d{ABSOLUTE} [%-5p] [%-8t] - %m%n" />
                </layout>
        </appender>

        <appender name="FOO_2"
                        class="org.apache.log4j.FileAppender">
                <param name="File" value="foo-2.log" />
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern"
                        value="%d{ABSOLUTE} [%-5p] [%-8t] - %m%n" />
                </layout>
        </appender>

Can you see what's happening? The foo-all.log file will contain *all*
log entries from all instances of Foo, whilst the files foo-1.log and
foo-2.log
will only contain log-entries from their respective instances.

Hope this helps.

--

Thomas





| -----Original Message-----
| From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
| Sent: 22 April 2002 12:23
| To: [EMAIL PROTECTED]
| Cc: [EMAIL PROTECTED]
| Subject: RE: Different logger objects
|
|
| Yes, but I need to recall it because I am taking some information
| from the properties file that changes from one logger to the
| other, like the Log file name. Do you know if possible to change
| this log filename without re-reading the "log4j.properties" file?
|
| -----Original Message-----
| From: ext Scott Farquhar [mailto:[EMAIL PROTECTED]]
| Sent: 22. April 2002 13:20
| To: Log4J Users List
| Subject: Re: Different logger objects
|
|
| You only need to call PropertyConfigure.configure() once per thread.
|
| This could be the source of your problem.
|
| Cheers,
| Scott
|
| [EMAIL PROTECTED] wrote:
| > Hi all,
| >
| > Is it possible to define two or more different logger objects
| in one Java Program? I am trying to create an own logger for each
| created object in the program, but using the instance
| Logger.getLogger(name) makes this no possible because when I
| create the second instance I "loose" the reference to the first
| one, probably because using static variables that changes and
| each object is using. Don't know how to do...
| >
| > I have tested if it's possible with the following example code:
| >
| >     this.logger1 = Logger.getLogger(MyClass.class.getName()+name1);
| >     PropertyConfigurator.configure("log4j.properties");
| >     logger1.info("- Logging initialized --");
| >
| >     this.logger2 =
| Logger.getLogger(ApiTesterTool.class.getName()+name2);
| >     PropertyConfigurator.configure("log4j.properties");
| >     logger2.info("- Logging initialized --");
| >
| >     // Till here the code works perfectly, the first logging
| information is written in one file and the other in the other one
| >     // Next line pretends to write again in logger1
| >
| >     logger1.info("This line must be written in file defined by
| logger1 but is written in logger2 !!!!!!!!");
| >
| > Please help a poor Java programmer ;-)
| >
| > Jaume
| >
| > --
| > To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
>


--
Scott Farquhar :: [EMAIL PROTECTED]

Atlassian :: http://www.atlassian.com
      Supporting YOUR J2EE World



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>




*************************************************************************
Copyright ERA Technology Ltd. 2002. (www.era.co.uk). All rights reserved. 
The information supplied in this email should be treated in confidence.
No liability whatsoever is accepted for any loss or damage 
suffered as a result of accessing this message or any attachments.

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to