日誌工廠類java
package cn.itcast.utils; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; /** * 用靜態方法封裝的Logger工廠類 * * @author weizh_000 * */ public class LoggerFactory { /** * 生成新的Logger對象 * @return 返回日誌對象 */ public static Logger createLogger() { Logger logger = Logger.getLogger(getCallerName()); try { Handler handler = new FileHandler("C:/Users/weizh_000/Desktop/log.log", true); handler.setFormatter(new SimpleFormatter());// 設置日誌的格式 logger.addHandler(handler);// 設置操做日誌的對象 } catch (Exception e) { e.printStackTrace(); } return logger; } /** * 獲取調用者類名 * @return 返回調用者類名 */ private static String getCallerName() { Throwable t = new Throwable(); StackTraceElement caller = t.getStackTrace()[1]; return caller.getClassName(); } }
調用方法:spa
public class TestCalc { private Logger logger = LoggerFactory.createLogger(); }