logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: Beginners question: log4j implementation, variable log4j properties
Date Sat, 11 Apr 2009 04:36:58 GMT
Please don't post user questions to the dev list.  I've sent this to the user
list, Bcc'ing the dev list, just in case you are not subscribed to the user list
yet.  See comments below...

On 4/10/2009 7:03 AM, mb77 wrote:
> Hello everybody, 
> 
> Beginners question here, hope all will be clear enough and log4j specific
> enough.
> 
> I'm co-building an application that consists of around thirty java classes
> that all use log4j.
> We have a class called MyLogger.java that is imported by all these classes.
> In MyLogger we have this line :
> public static final org.apache.log4j.Logger log =
> Logger.getLogger("MyLogger.class");
> 
> We use a log4jproperties file for now ,but the finished application will
> take care that every user's log4j properties are read from his/her own
> inifile (using PropertyConfigurator). At a certain point in our code we
> determine which user we are dealing with , but until that time we would like
> to use a default inifile.
> 
> As stated above , the logging object is created like this:
> public static final org.apache.log4j.Logger log =
> Logger.getLogger(RemLogger.class);
> As far as i understand this object is created the first time one of our
> classes imports the MyLogger class.
> 
> Would it be possible somehow to tweak this code so that the constructor of
> MyLogger is called? 

No.  All that happens when you pass a class into getLogger() is it gets the name
of the class by calling Class.getName().

> In the constructor i would place something like this:
> URL url = Loader.getResource("default.ini");
> PropertyConfigurator.configure(url);

I really don't recommend that.  General application code should not perform
configuration.  Leave that to specialized startup classes.  Either place
log4j.properties or log4j.xml in the root of the classpath for Log4j
auto-configuration or run manual configuration from something like a
ServletContextListener or a Main class.

You will find nothing but grief by reconfiguring the current logger repository
(generally the default one unless you are using repository selector.... which I
bet you are not) multiple times as you think you want; once per ever time you call
Logger.getLogger(SomeClass.class).  You'll end up doubling up appenders, getting
duplicate, triplicate, quadruplicate, etc... logging.

Remember.  It is not really the job of the library developer to decide which stuff
gets logged.  It is the application deployer (possibly in consulation with
developers).  Individual libraries my provide information by logging it, but it's
the person(s) who read the log that ought to control what actually gets logged.

> 
> Hope i made my question clear and would appreciate any help immensely!!
> Thanks in advance,
> Mark 
> 
> 

Jake

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message