使用java實現(xiàn)日志工具類分享
package com.teligen.eos.teleCode;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
/**
* 書寫日志信息到指定的文件中
*/
public class WriteLogUtil {
private static String rootPath = "D:\\logs\\";
/**
* 將信息寫到文件中
* @param msg
*/
public static void writeMsgToFile(String msg) {
//刪除之前的文件
delOldFile();
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(getFileName(),true);
Date today = new Date();
String time = String.valueOf(today.getHours()) + ":" + String.valueOf(today.getMinutes()) + " " + String.valueOf(today.getSeconds());
fileWriter.write("#" + time + "# [" + msg + "]" + "\r\n");
fileWriter.flush();
} catch (IOException e) {
System.out.println("### 寫日志到文件異常 ### >>> " + e.getMessage());
e.printStackTrace();
} finally {
try {
fileWriter.close();
} catch (IOException e) {
System.out.println("### 關(guān)閉寫日志的流異常 ### >>> " + e.getMessage());
e.printStackTrace();
}
}
}
/**
* 刪除之前的日志文件
*/
private static void delOldFile() {
Date today = new Date();
int month = today.getMonth()+1;
month = month - 2;
if(month == -1) month = 11;
if(month == 0) month = 12;
String delPath = rootPath + String.valueOf(month) + "\\";
File folder = new File(delPath);
if(folder.exists()) {
File[] files = folder.listFiles();
for(int i=0; i<files.length; i++) {
files[i].delete();
}
}
}
/**
* 獲取要保存的文件
* @return fileName
*/
private static String getFileName() {
Date today = new Date();
String fileName = String.valueOf((today.getYear()+1900)) + String.valueOf((today.getMonth()+1)) + String.valueOf(today.getDate()) + ".log";
//創(chuàng)建目錄
File folder = new File(rootPath + String.valueOf((today.getMonth()+1)) + "\\");
if(!folder.exists()) {
folder.mkdirs();
}
//創(chuàng)建文件
File file = new File(fileName);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("### 新建日志文件異常 ### >>> " + e.getMessage());
e.printStackTrace();
}
}
fileName = rootPath + String.valueOf((today.getMonth()+1)) + "\\" + fileName;
return fileName;
}
/**
* 測試使用的main方法
*/
public static void main(String[] args) {
//getFileName();
String testString = "寫日志咯:71FABB7890D2CC0D267FBD84F409618C0303BC597B9244C324947BDE4B1C0B4CB08C33FC461F7BADD088535DAE42D8D7D06F4134E442D9D1CE3A0F9B3EDD64337A2D18CE34FCDC137B7CBD84F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F581790ACB3C178641D14D8C09905BC52CF1C8249B12F2EDE5AC3C8FAF2FD8A686E";
writeMsgToFile(testString);
//delOldFile();
}
}
相關(guān)文章
springmvc fastjson 反序列化時間格式化方法(推薦)
下面小編就為大家?guī)硪黄猻pringmvc fastjson 反序列化時間格式化方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代碼
這篇文章給大家介紹了SpringBoot使用classfinal-maven-plugin插件加密Jar包的實例,文中通過代碼示例和圖文講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02SpringBoot下實現(xiàn)session保持方式
這篇文章主要介紹了SpringBoot下實現(xiàn)session保持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot集成P6spy實現(xiàn)自定義SQL日志打印
本文主要介紹了SpringBoot集成P6spy實現(xiàn)自定義SQL日志打印,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringBoot org.springframework.beans.factory.Unsatisfie
本文主要介紹了SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常,文中通過示例代碼介紹的很詳細(xì),具有一定的參考價值,感興趣的可以了解一下2024-02-02SpringBoot設(shè)置接口超時的方法小結(jié)
這篇文章主要介紹了SpringBoot設(shè)置接口超時的方法小結(jié),包括配置文件,config配置類及相關(guān)示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09java 全角半角字符轉(zhuǎn)換如何實現(xiàn)
在java中可能會用到過全角半角字符轉(zhuǎn)換問題,于是網(wǎng)上搜索整理了一下,曬出來和大家分享,希望可以幫助你們2012-12-12阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式
本文主要介紹了阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式,文中根據(jù)實例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03