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 Andy Kriger <ak...@triene.com> on 2001/01/29 22:14:24 UTC
PropertyConfigurator question
I have a wrapper class for log4j. Enclosed is some code I use to test the
wrapper...
public class LoggerTest
{
public static void main(String[] args) {
Object[] obj = {
new A(),
new B(),
new C(),
new D()
};
try {
for(int i = 0; i < obj.length; i++) {
TrieneLogger.getInstance().info(obj[i], "The
quick brown fox jumped over the info dog.");
TrieneLogger.getInstance().warn(obj[i], "The
quick brown fox jumped over the warn dog.");
TrieneLogger.getInstance().error(obj[i],
"The quick brown fox jumped over the error dog.");
TrieneLogger.getInstance().debug(obj[i],
"The quick brown fox jumped over the debug dog.");
}
throw(new Exception("log this buddy!"));
}
catch(Exception e) {
for(int i = 0; i < obj.length; i++) {
TrieneLogger.getInstance().info(obj[i], "The
quick brown fox jumped over the info dog.", e);
TrieneLogger.getInstance().warn(obj[i], "The
quick brown fox jumped over the warn dog.", e);
TrieneLogger.getInstance().error(obj[i],
"The quick brown fox jumped over the error dog.", e);
TrieneLogger.getInstance().debug(obj[i],
"The quick brown fox jumped over the debug dog.", e);
}
}
}
}
class A {}
class B {}
class C {}
class D {}
// EOF
The getInstance method calls the empty constructor...
public TrieneLogger() {
// init instance
instance = this;
// load properties
BasicConfigurator.configure();
}
If I do not call BasicConfigurator.configure() and a log4j.properties file
cannot be found, I get the following error from the first time
TrieneLogger.getInstance().info(...) is called because I haven't loaded any
configurator but I am trying to log.
log4j:ERROR No appenders could be found for category (A).
log4j:ERROR Please initialize the log4j system properly.
However, if I do call BasicConfigurator.configure() to cover myself and a
log4j.properties file is found, then there is duplicate logging to
System.out because BasicConfig is logging and the properties file defines a
customized console appender. This creates a messy confusion that I do not
want to force my team to wade through.
What I want is a way to check if PropertyConfigurator is able to find
log4j.properties and if not, then and only then call
BasicConfigurator.configure() as a backup.
Is this possible?
Andy Kriger
Triene Inc.
116 W23rd Street
Suite 500
New York, NY 10011
646.375.2364 (W)
646.298.5493 (M)
Re: PropertyConfigurator question
Posted by Anders Kristensen <ak...@dynamicsoft.com>.
Andy Kriger wrote:
> [...]
> What I want is a way to check if PropertyConfigurator is able to find
> log4j.properties and if not, then and only then call
> BasicConfigurator.configure() as a backup.
>
> Is this possible?
You can check whether at least one Category has been added to the
default hierarchy or whether an appender has been added to the root
category:
public static boolean isLog4jConfigured() {
Enumeration cats = Category.getCurrentCategories();
Enumeration rootApps = Category.getRoot().getAllAppenders();
return cats.hasMoreElements() || rootApps != null;
}
I haven't checked it but it ought to work.
>
> Andy Kriger
> Triene Inc.
> 116 W23rd Street
> Suite 500
> New York, NY 10011
> 646.375.2364 (W)
> 646.298.5493 (M)
>
> ------------------------------------------------------------------------
>
> Part 1.2 Type: application/ms-tnef
> Encoding: base64
--
Anders Kristensen