一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - 自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享

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

2019-12-23 15:21junjie JAVA教程

這篇文章主要介紹了自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 *
 */
 
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();
      }
    }   
  }
 
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人天堂官方网站 | 久久国产乱子伦免费精品 | 91久久国产青草亚洲 | 欧美成a人片免费看久久 | 国产资源免费观看 | 日韩毛片基地一区二区三区 | 国产成人精品视频一区二区不卡 | 精品亚洲午夜久久久久 | 国产不卡视频 | japanhd粗暴video | 日本老妇和子乱视频 | 国色天香社区视频免费高清在线观看 | 国产精品国产香蕉在线观看网 | 欧美人人干 | 6080欧美一区二区三区四区 | 精选国产AV精选一区二区三区 | 明星ai人脸替换脸忘忧草 | 倩女还魂在线观看完整版免费 | 成人网中文字幕色 | 500福利第一导航 | 色亚 | 欧美在线观看一区二区三 | 日本动漫xxxxxx| 四虎国产精品免费入口 | 亚洲国内精品 | 成年人福利视频 | 国产精品一区二区不卡的视频 | 我与恶魔的h生活ova | 国产一卡2卡3卡四卡精品网站 | 国产视频一区 | 国产一卡2卡3卡四卡精品网 | 深夜成人 | 免费看黄色一级 | 亚洲国产高清视频 | 国产一二区视频 | 国产专区视频在线观看 | 日本成熟 | 国产精品99在线观看 | 欧美成年黄网站色高清视频 | 亚洲欧美在线免费 | 99热99在线 |