国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

自己寫的簡易版Java日志類分享

2019-11-26 15:08:25
字體:
來源:轉載
供稿:網友
/** *  */ import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date; /** * @author magic282 * */public class Logger {  private static String logFilePath;  private static boolean isInitialized = false;  private static FileWriter logWriter = null;  private static boolean printLogWhenLog = true;   private static boolean InitLogger() {    String logDirectoryPath = System.getProperty("user.dir")        + java.io.File.separatorChar + "log";     if (!new File(logDirectoryPath).exists()) {      new File(logDirectoryPath).mkdir();    }    Date logfileDate = new Date();    SimpleDateFormat dateFormat = new SimpleDateFormat(        "yyyy-MM-dd-HH-mm-ss");    logFilePath = logDirectoryPath + java.io.File.separatorChar        + dateFormat.format(logfileDate) + ".log";     try {      logWriter = new FileWriter(logFilePath, true);      isInitialized = true;    } catch (IOException e) {      // TODO Auto-generated catch block      System.err.println("Unable to create log file.");      System.err.println("Initilization fail.");      e.printStackTrace();      return false;    }    return true;  }   public static void Log(String message) {    if (!isInitialized) {      InitLogger();    }    Date logfileDate = new Date();    SimpleDateFormat dateFormat = new SimpleDateFormat(        "yyyy-MM-dd-HH-mm-ss");    String callingClassName = new Exception().getStackTrace()[1]        .getClassName();    synchronized (logWriter) {      String log = String.format("[%s] @ [%s]: %s/n", callingClassName,          dateFormat.format(logfileDate), message);      if (printLogWhenLog) {        System.out.printf("[log]:%s", log);      }      try {        logWriter.write(log);        logWriter.flush();      } catch (IOException e) {        // TODO Auto-generated catch block        System.err.println("Write log to file %s error.");        e.printStackTrace();      }    }  }   public static void Log(Exception exception) {    if (!isInitialized) {      InitLogger();    }    Date logfileDate = new Date();    SimpleDateFormat dateFormat = new SimpleDateFormat(        "yyyy-MM-dd-HH-mm-ss");    String callingClassName = new Exception().getStackTrace()[1]        .getClassName();    synchronized (logWriter) {      String log = String.format("[%s] @ [%s]: %s/n", callingClassName,          dateFormat.format(logfileDate), exception.toString());      if (printLogWhenLog) {        System.out.printf("[log]:%s", log);      }      try {        logWriter.write(log);        logWriter.flush();      } catch (IOException e) {        // TODO Auto-generated catch block        System.err.println("Write log to file %s error.");        e.printStackTrace();      }    }      } }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 东乡族自治县| 扎赉特旗| 玉溪市| 富裕县| 玉环县| 托克逊县| 巢湖市| 许昌市| 神池县| 封丘县| 基隆市| 曲沃县| 德安县| 商水县| 昭平县| 开平市| 于田县| 望谟县| 准格尔旗| 嘉荫县| 滨海县| 新民市| 乌鲁木齐县| 莱西市| 平乡县| 库尔勒市| 博爱县| 克拉玛依市| 乐至县| 藁城市| 成安县| 邵武市| 齐齐哈尔市| 田林县| 山阴县| 江华| 福贡县| 泊头市| 宁津县| 营口市|