You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Ex...@nokia.com on 2002/04/22 13:02:59 UTC

Different logger objects

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Different logger objects

Posted by Scott Farquhar <sc...@atlassian.com>.
You only need to call PropertyConfigure.configure() once per thread.

This could be the source of your problem.

Cheers,
Scott

Ext-Jaume.ribot@nokia.com 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:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


-- 
Scott Farquhar :: scott@atlassian.com

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



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>