posted 13 years ago
In a multithreaded application, can a logger (log4j) instance be static?
I have a static method that uses logging messages, something like the code below
Public Foo{
private static Logger logger = Logger.getLogger(Foo.class.getName());
public static boolean doSomething(Boo boo){
....do something with boo.....
logger.debug("something happened");
return true/false;
}
If I want the method to be static, I have to declare my logger instance to be static. Since static variables are 1 per class and will be shared among all the instances of the class Foo, can we declare the logger instance as static?. If all the instances of class Foo try to log the messages to the one logger instance, it may cause problems, like dirty writes, am I correct? I don't want to make the method or logger instance synchronized, that's gonna hit the performance big time. In such case how to deal with the static methods that need logging in multithreaded app.
Appreciate much your replies.
Thanks,
Jack.