欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年06月16日 10:57:44   投稿:junjie  
這篇文章主要介紹了自己寫的簡易版Java日志類分享,本文直接給出實現(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)文章

  • SpringBoot攔截器Filter的使用方法詳解

    SpringBoot攔截器Filter的使用方法詳解

    這篇文章主要介紹了SpringBoot攔截器Filter的使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java移動文件夾及其所有子文件與子文件夾

    Java移動文件夾及其所有子文件與子文件夾

    這篇文章主要為大家詳細(xì)介紹了Java移動文件夾及其所有子文件與子文件夾的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 淺談幾種Java自定義異常處理方式

    淺談幾種Java自定義異常處理方式

    在Java中,異常是一種常見的處理機(jī)制,本文主要介紹了淺談幾種Java自定義異常處理方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • java agent 使用及實現(xiàn)代碼

    java agent 使用及實現(xiàn)代碼

    java agent的作用可以在字節(jié)碼這個層面對類和方法進(jìn)行修改的技術(shù),能夠在不影響編譯的情況下,修改字節(jié)碼。本文主要給大家講解java agent 使用及實現(xiàn)代碼,感興趣的朋友一起看看吧
    2018-07-07
  • 簡單聊一聊Spring中Bean別名的處理原理

    簡單聊一聊Spring中Bean別名的處理原理

    今天來和小伙伴們聊一聊 Spring 中關(guān)于 Bean 別名的處理邏輯,別名,顧名思義就是給一個 Bean 去兩個甚至多個名字,整體上來說,在 Spring 中,有兩種不同的別名定義方式,感興趣的小伙伴跟著小編一起來看看吧
    2023-09-09
  • Java 實例解析單例模式

    Java 實例解析單例模式

    單例模式(Singleton Pattern)是 Java 中最簡單的設(shè)計模式之一。這種類型的設(shè)計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,這種模式涉及到一個單一的類,該類負(fù)責(zé)創(chuàng)建自己的對象,同時確保只有單個對象被創(chuàng)建
    2021-11-11
  • JAVA使用ffmepg處理視頻的方法(壓縮,分片,合并)

    JAVA使用ffmepg處理視頻的方法(壓縮,分片,合并)

    這篇文章主要介紹了JAVA使用ffmepg處理視頻的方法,包括視頻壓縮分片合并功能,通過實例代碼講解的很詳細(xì),對java ffmepg處理視頻相關(guān)知識感興趣的朋友一起看看吧
    2021-05-05
  • java調(diào)用shell腳本及注意事項說明

    java調(diào)用shell腳本及注意事項說明

    這篇文章主要介紹了java調(diào)用shell腳本及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 淺談一下Java中的堆和棧

    淺談一下Java中的堆和棧

    這篇文章主要介紹了一下Java中的堆和棧,Java數(shù)據(jù)類型在執(zhí)行過程中存儲在兩種不同形式的內(nèi)存中:棧和堆,它們通常由運行Java虛擬機(jī)(JVM)的底層平臺維護(hù),需要的朋友可以參考下
    2023-04-04
  • 國內(nèi)分布式框架Dubbo使用詳解

    國內(nèi)分布式框架Dubbo使用詳解

    這篇文章主要為大家介紹了國內(nèi)分布式框架Dubbo使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評論