Setting the configuration in classes that don't contain a main().
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rob_Fleming
Posted On:   Thursday, January 2, 2003 02:00 PM

How do I go about setting the configuration for a logger where the class doesn't have a main() method? In the examples that I have looked at the logger is statically instantiated and then the configuration is set in main(). For example: public class MyApp { static Logger logger = Logger.getLogger(MyApp.class); public static void main(String[] args) { PropertyConfigurator.configure(...); " " " " } } However, my classes don't contain a main(). I have written a small demo where I statically instantiate a Logger and then I use a static intialization block to set the configuration in each class. It looks   More>>


How do I go about setting the configuration for a logger where the class doesn't have a main() method? In the examples that I have looked at the logger is statically instantiated and then the configuration is set in main().
For example:



			
public class MyApp {

static Logger logger = Logger.getLogger(MyApp.class);

public static void main(String[] args) {
PropertyConfigurator.configure(...);
" "
" "
}
}



However, my classes don't contain a main(). I have written a small demo where I statically instantiate a Logger and then I use a static intialization block to set the configuration in each class. It looks like:



			
public class MyClass{

static Logger logger = Logger.getLogger(MyClass.class);

static {
PropertyConfigurator.configureAndWatch(
"SomeFile.properties", 1500);
}
}




Is this a bone-head thing to do?

   <<Less

Re: Setting the configuration in classes that don't contain a main().

Posted By:   Mark_Womack  
Posted On:   Friday, January 3, 2003 04:57 PM

You can look to using the default initialization procedure provided by log4j. You can find a description of it in this url:

http://jakarta.apache.org/log4j/docs/manual.html

Search for "default initialization". Unfortunately, there is no way to do a configureAndWatch() using this method in v1.2.7. We are looking to add a "watchdog" capability to v1.3 that will be configurable from a configuration file.

It is probably dangerous to do it the way you have above, in a static block. You will need to guarantee that there is just one static block that does the above code.

-Mark
About | Sitemap | Contact