国产探花免费观看_亚洲丰满少妇自慰呻吟_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();      }    }      } }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿勒泰市| 阜阳市| 东台市| 东方市| 四平市| 丹凤县| 舟山市| 绵竹市| 东乌珠穆沁旗| 崇文区| 泸水县| 多伦县| 邓州市| 阜阳市| 房产| 台中市| 峨山| 安国市| 辛集市| 韩城市| 奉贤区| 民勤县| 光山县| 高雄市| 伽师县| 弋阳县| 晋宁县| 偏关县| 峡江县| 县级市| 姜堰市| 邵武市| 新巴尔虎右旗| 通山县| 灌阳县| 宁波市| 志丹县| 万荣县| 司法| 贡山| 吉木乃县|