【发布时间】:2012-01-18 18:35:33
【问题描述】:
在我的应用程序中,我使用 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;
}
}
我需要对 getLogger 方法使用同步吗?请给你的cmets。 (此代码运行在多线程环境中)
【问题讨论】:
标签: java multithreading logging