你的分享就是我们的动力 ---﹥

java.util.logging.Logger的properties配置文件使用

时间:2013-07-16 14:59来源:www.chengxuyuans.com 点击:

代码简介

日志管理系统主要使用log4j和jdk自带的Logger。对于log4j,网上有一大把的例子,这里及不做演示了。

代码片段

/**
 * 日志{@link java.util.logging.Logger}获取器
 */
public class Logging {
	private static Logger logger = null;
	private Logging(){}
	
	public static Logger getLogger(){
		if (null == logger) {
			InputStream is  = Logging.class.getClass().getResourceAsStream("/logger.properties");
			try {
				LogManager.getLogManager().readConfiguration(is);
			} catch (Exception e) {
				logging.warning("input properties file is error.\n" + e.toString());
			}finally{
				try {
					is.close();
				} catch (IOException e) {
					logging.warning("close FileInputStream a case.\n" + e.toString());
				}
			}
			
			logger = Logger.getLogger("LOGGER");
		}
		return logger;
	}
	
	private static Logger logging = Logger.getLogger(Logging.class.getName());
}

代码片段

public class LoggerTest extends TestCase{
	@Test
	public void testLogger() throws Exception {
		Logger logger = Logging.getLogger();
		logger.finest("finest");
		logger.finer("finer");
		logger.fine("fine");
		logger.info("info");
		logger.config("config");
		logger.warning("warning");
		logger.severe("severe");
		
	}
}

转载注明地址http://www.chengxuyuans.com/code/java/65821.html