自己寫的簡易版Java日志類分享
更新時(shí)間:2015年06月16日 10:57:44 投稿:junjie
這篇文章主要介紹了自己寫的簡易版Java日志類分享,本文直接給出實(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)文章
springboot結(jié)合mybatis-plus基于session模擬短信注冊功能
本文主要介紹了springboot結(jié)合mybatis-plus基于session模擬短信注冊功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
spring security結(jié)合jwt實(shí)現(xiàn)用戶重復(fù)登錄處理
本文主要介紹了spring security結(jié)合jwt實(shí)現(xiàn)用戶重復(fù)登錄處理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)
這篇文章主要介紹了Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例),需要的朋友可以參考下2017-09-09
MyBatis入門實(shí)例教程之創(chuàng)建一個(gè)簡單的程序
這篇文章主要介紹了MyBatis入門創(chuàng)建一個(gè)簡單的程序,在?MySQL?中創(chuàng)建數(shù)據(jù)庫?mybatisdemo,編碼為?utf8,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Java利用LocalDate進(jìn)行日期處理的完全指南
這篇文章主要為大家詳細(xì)介紹了Java利用LocalDate進(jìn)行日期處理的詳細(xì)教程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03

