`
欧阳晓
  • 浏览: 44464 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

日志模块

阅读更多

      下面是一个简单的日志模块,主要功能是用一个TXT文本记录一些异常输出,避免过多的使用system.out.println();,还有就是便于更加彻底的寻找BUG源。

      package cn.netjava.Logtools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

//日志模块
public class Logtools {
 public Logtools(){}
 //记录消息的方法
 public static void info(String msg){
  //将消息写入一个文件中
  //得到当前系统的时间
  Calendar now=Calendar.getInstance();
  String time=now.get(Calendar.YEAR)+"-"+now.get(Calendar.MONTH+1)+"-"+now.get(Calendar.DAY_OF_MONTH)
  +" "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND);
  
  File file=new File("D:\\java1\\ManageSystem\\WebRoot\\note.txt");
  if(file.exists()){
   try{
   java.io.BufferedWriter buf=new BufferedWriter(new FileWriter(new File("D:\\java1\\ManageSystem\\WebRoot\\note.txt"),true));
   buf.write(time+"----"+msg+"\r\n");
   buf.flush();
   buf.close();
   }catch(Exception e){
    e.fillInStackTrace();
   }
  }else if(!file.exists()){
   try {
    file.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
   
  }
 }

}
     

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics