java 自己写的保存日志

package com.dfkj.log;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyLog {
 /**
  *
  * @param info 内容
  * @param type info
  */
 public static void  log(String str,String type) {
  try {
   
   //创建文件start
   java.io.File logfile = new java.io.File("d://",
     "log");
   if (!logfile.exists()) {
    logfile.mkdirs();
   }
   SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
   String timePath = sdf2.format(new Date());
   
   File saveFileurl = new File(timePath+".txt");
   File saveFile = new File(logfile+"/"+saveFileurl);
   if (!saveFile.exists()) {
    saveFile.createNewFile();
   }
   //创建文件end
   FileWriter writer = new FileWriter(saveFile,true);
   writer.write(new Date().toLocaleString()+"--------"+type+"--"+str+"\r\n");
   writer.flush();
   writer.close();
   //向文件中写数据end
  
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 public static void main(String[] args) {
  MyLog.log("asdfasdfasdf你好!阿是飞洒发!#@324","info");
  MyLog.log("asdfasdfasdf你好!阿是飞洒发!#@324","debug");
 }

}

你可能感兴趣的:(java,日志)