In my application I have written my own Logging utility using Java.Util.Logging
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.SimpleFormatter;
public class Logger {
public final static String PROPERTIES_FILE = "Logger.properties";
private static java.util.logging.Logger logger = null;
private static void initialize() {
try {
logger = java.util.logging.Logger.getLogger(Logger.class.getName());
FileHandler fh = new FileHandler("D:\\MyLogFile.log", true);
logger.addHandler(fh);
logger.setLevel(Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.log(Level.INFO, "Test Message Logged");
}
catch (IOException e) {
System.out.println("Warning: Unable to read properties file " +
PROPERTIES_FILE );
}
}
public static synchronized java.util.logging.Logger getLogger(String name)
{
if(logger==null)
{
Logger.initialize();
}
logger.getLogger(name);
return logger;
}
}
Do I need to use Synchronization for getLogger method? Please give your comments. (This code is running in Multi-threaded environment)
I agree with other commenters that lazy initialization doesn't seem necessary here. The simplest way to initialize the logger variable is in a static initializer, which is guaranteed to only execute once at class loading time:
However, You can avoid most synchronization with double-checked locking.
In fact, in your case I think you can avoid synchronization altogether if you rewrite your initialize function so that it completely configures the logger in an local variable prior to assigning it to the (volatile) class variable:
This has a potential to have initialize() execute several times, but I don't think you care, so long that every getLogger invocation will have a valid logger instance, even if that instance varies.
I wouldn't recommend it, because you'll be incurring the overhead of synchronization on every call to
getLogger()
when you only need it once for initialization. I'd do this instead:This way, if a second thread comes in while the first thread is still in initialize(), the second thread will block because of synchronized on initialize(). Once the first thread is done with initialization, the second thread proceeds, sees that initialized is true, doesn't redo the initialization, and moves on.
Yes, you need
synchronized
there. First to avoid callinginitialize()
several times and secondly to makelogger
change visible to other threads.This begs a question: why can't you eagerly
initialize()
logger and avoid synchronization altogether?