最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享

 更新時(shí)間:2015年06月16日 10:57:44   投稿:junjie  
這篇文章主要介紹了自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
/**
 * 
 */
 
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();
      }
    }    
  }
 
}

相關(guān)文章

  • 詳解Spring的StringUtils踩坑記錄

    詳解Spring的StringUtils踩坑記錄

    這篇文章主要介紹了詳解Spring的StringUtils踩坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能

    springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能

    本文主要介紹了springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • idea如何自動(dòng)生成serialVersionUID

    idea如何自動(dòng)生成serialVersionUID

    這篇文章主要介紹了idea如何自動(dòng)生成serialVersionUID,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • JAVA多線(xiàn)程編程實(shí)例詳解

    JAVA多線(xiàn)程編程實(shí)例詳解

    這篇文章主要介紹了JAVA多線(xiàn)程編程,結(jié)合實(shí)例形式總結(jié)分析了多線(xiàn)程、鎖、線(xiàn)程池等相關(guān)原理及使用技巧,需要的朋友可以參考下
    2019-09-09
  • 前端和后端時(shí)間不一致問(wèn)題解決的實(shí)踐指南

    前端和后端時(shí)間不一致問(wèn)題解決的實(shí)踐指南

    這篇文章主要給大家介紹了關(guān)于前端和后端時(shí)間不一致問(wèn)題解決的實(shí)踐指南,在SpringBoot項(xiàng)目中,可以通過(guò)設(shè)置application.yml文件中的屬性來(lái)統(tǒng)一時(shí)間格式和時(shí)區(qū),從而確保序列化和反序列化過(guò)程中的時(shí)間和時(shí)區(qū)一致性,需要的朋友可以參考下
    2025-01-01
  • spring security結(jié)合jwt實(shí)現(xiàn)用戶(hù)重復(fù)登錄處理

    spring security結(jié)合jwt實(shí)現(xiàn)用戶(hù)重復(fù)登錄處理

    本文主要介紹了spring security結(jié)合jwt實(shí)現(xiàn)用戶(hù)重復(fù)登錄處理,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)

    Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)

    這篇文章主要介紹了Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例),需要的朋友可以參考下
    2017-09-09
  • MyBatis入門(mén)實(shí)例教程之創(chuàng)建一個(gè)簡(jiǎn)單的程序

    MyBatis入門(mén)實(shí)例教程之創(chuàng)建一個(gè)簡(jiǎn)單的程序

    這篇文章主要介紹了MyBatis入門(mén)創(chuàng)建一個(gè)簡(jiǎn)單的程序,在?MySQL?中創(chuàng)建數(shù)據(jù)庫(kù)?mybatisdemo,編碼為?utf8,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Java使用注解和反射簡(jiǎn)化編程的方法示例

    Java使用注解和反射簡(jiǎn)化編程的方法示例

    這篇文章主要介紹了Java使用注解和反射簡(jiǎn)化編程的方法,結(jié)合實(shí)例形式分析了java使用注解和反射調(diào)用大量函數(shù)簡(jiǎn)化編程的相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • Java利用LocalDate進(jìn)行日期處理的完全指南

    Java利用LocalDate進(jìn)行日期處理的完全指南

    這篇文章主要為大家詳細(xì)介紹了Java利用LocalDate進(jìn)行日期處理的詳細(xì)教程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03

最新評(píng)論

木兰县| 礼泉县| 平邑县| 衡水市| 西青区| 华亭县| 茶陵县| 乌拉特后旗| 汕头市| 东宁县| 白银市| 吉首市| 枣强县| 凌海市| 惠东县| 拜城县| 宜丰县| 三原县| 平安县| 榆社县| 诸城市| 大英县| 张掖市| 长寿区| 招远市| 潜江市| 思南县| 井陉县| 微博| 西丰县| 乐陵市| 黑山县| 咸阳市| 柯坪县| 邛崃市| 南岸区| 余庆县| 雅安市| 奉节县| 镇巴县| 兰考县|