使用java實現日誌工具類分享_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

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("### 關閉寫日誌的流異常 ### >>> " + 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";

  
  //建立目錄
  File folder = new File(rootPath + String.valueOf((today.getMonth()+1)) + "\\");
  if(!folder.exists()) {
   folder.mkdirs();
  }
  //建立檔案
  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();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.