自己寫的java日志類和方法代碼分享
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
public class AndyLogger
{
//The defaulted root path of SSLVPN installation
private static String rootPath = "C:\\temp2";
//variable for creating new line
private final static String enter = System.getProperty("line.separator");
private static SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static synchronized void log(String fileName, String logMessage)
{
try
{
File folder = new File(rootPath);
if(!folder.exists())
{
folder.mkdir();
}
File file = new File(rootPath + "\\" + fileName + ".log");
if(!file.exists())
{
file.createNewFile();
}
BufferedReader in = new BufferedReader(new FileReader(file));
String str = "";
String strToal = "";
while ((str = in.readLine()) != null)
{
strToal += (str + enter);
}
strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(strToal);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static synchronized void log(String fileName, String[] logMessages)
{
try
{
File folder = new File(rootPath);
if(!folder.exists())
{
folder.mkdir();
}
File file = new File(rootPath + "\\" + fileName + ".log");
if(!file.exists())
{
file.createNewFile();
}
BufferedReader in = new BufferedReader(new FileReader(file));
String str = "";
String strToal = "";
while ((str = in.readLine()) != null)
{
strToal += (str + enter);
}
for (int i=0; i < logMessages.length ; i++)
{
String logMessage = logMessages[i];
strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(strToal);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
AndyLogger.log("bug223", "timeisjjja");
String[] logMessages = {"111","222","333"};
AndyLogger.log("bug223", logMessages);
}
}
相關(guān)文章
springboot自定義starter方法及注解實(shí)例
這篇文章主要為大家介紹了springboot自定義starter方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
SpringSecurity中的表單認(rèn)證詳細(xì)解析
這篇文章主要介紹了SpringSecurity中的表單認(rèn)證詳細(xì)解析,在上一篇文章中,我們初步引入了?Spring?Security,并使用其默認(rèn)生效的?HTTP?基本認(rèn)證保護(hù)?URL?資源,在本篇文章中我們使用表單認(rèn)證來保護(hù)?URL?資源,需要的朋友可以參考下2023-12-12
SpringBoot自定義注解及AOP的開發(fā)和使用詳解
在公司項(xiàng)目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定義注解,自定義注解可以實(shí)現(xiàn)我們對(duì)想要添加日志的方法上添加,這篇文章基于日志功能來講講自定義注解應(yīng)該如何開發(fā)和使用,需要的朋友可以參考下2023-08-08
Java Thread多線程開發(fā)中Object類詳細(xì)講解
這篇文章主要介紹了Java Thread多線程開發(fā)中Object類,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03
SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)權(quán)限校驗(yàn)的過程
Spring Security過濾器鏈中,AuthorizationFilter的authorizationManager是我們要找的組件,該組件的check方法已被棄用,推薦使用authorize方法,最終通過接口路徑和權(quán)限進(jìn)行校驗(yàn),本文給大家介紹SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)權(quán)限校驗(yàn)的相關(guān)知識(shí),感興趣的朋友一起看看吧2025-02-02
Spring?Cloud?Hystrix原理與注意事項(xiàng)小結(jié)
本文介紹了Hystrix的基本概念、工作原理以及其在實(shí)際開發(fā)中的應(yīng)用方式,通過對(duì)Hystrix的深入學(xué)習(xí),開發(fā)者可以在分布式系統(tǒng)中實(shí)現(xiàn)精細(xì)的錯(cuò)誤處理機(jī)制,并能夠及時(shí)響應(yīng)系統(tǒng)中的異常,避免服務(wù)的連鎖崩潰,感興趣的朋友一起看看吧2025-03-03

