静态日志操作类

 

import java.io.*;
import java.util.*;

/**
 * 类的具体使用:
 *
 * 1.把该类编译好, 新建立属性配置文件:log.properties,并确保把它放倒你的这个
 * 编译好的类所在的位置文件内容如下:当然你可以把路径修改为你想要的路径
 * logfile=e://logtext.log
 * 2.使用举例:
 * 使用1:
 * LogWriter.log("张三登陆了该系统");
 * logWriter.log("张三删除了xxx条记录:记录id:");
 * 使用2:
 * try{
 * }
 * catch (Exception ex) {
 *   LogWriter.log(ex);
 * }
 *
 * 几点说明:
 * 一.其中的 getClass().getResourceAsStream("文件名称")不支持static的调用,
 * 所以要把该类换为非static,但是它的调用仅仅在于outinit()中调用,而outinit()
 * 也仅仅在私有的构造函数中调用,而私有构造函数可以在静态的static 中被调用,
 * 这样就达到了可以利用静态方法来调用随时输入日志,并保证了仅仅有一个实例。
 * 二.如果你了解log4J的话,可以使用该类似方法,把log4j封装一下,实现静态方便的调用.
 *
 * <p>Title: 静态日志操作类</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Hanic
 * @version 1.0
 */

public class LogWriter {
  private static final String      DefalutLogFilePathName = "c://logtext.log";//默认的日志文件的路径和文件名称
  private static       LogWriter   logwriter;                                 //该类的唯一的实例
  private static       InputStream fin;                                       //属性配置文件的输入流
  private static       Properties  pro;                                       //class Properties′s supper is Hashtable class
  private static       PrintWriter out;                                       //output stream
  private static       String      logFileName;                               //output file name

  private LogWriter() {
    outInit();//init out put stream,实例化PrintWriter out 对象.
  }

  /**
   * 保存你想保存在日志文件中的信息,实现同步
   * out put the message infomation
   * @param message infomation
   */

  public static synchronized void log(String message) {
    if (logwriter == null || (out == null)){
      logwriter = new LogWriter();
    }
    if (out != null) {
      out.println(new java.util.Date() + ":" + message);
    }
  }

  /**
   * 把异常信息保存在日志文件中,实现同步
   * out put the Excetion infomation
   * @param ex
   */

  public static synchronized void log(Exception ex) {
    if (logwriter == null || (out == null))
      logwriter = new LogWriter();
    if (out != null) {
      out.println(new java.util.Date() + ":" );
      ex.printStackTrace(out);
    }
  }

  /**
   * 输出文件流的init
   */

  private void outInit() {
    if (logFileName == null){
      logFileName = getlogFileName(); //从属性文件中类获得日志文件的路径
    }
    try {
      if (out == null) {//如果输出i/o没有实例,则生成一个新的
        out = new PrintWriter(new FileWriter(logFileName, true), true);
        //其中的FileWriter()中的第二个参数的含义是:是否在文件中追加内容
      }
    }
    catch (IOException ex) {
      System.out.println("无法打开日志文件:"+logFileName);
      ex.printStackTrace();
      out = null;
    }
  }

  /**
   * 根据配置文件.来获得日志文件的位置
   *
   * @return logFileName
   */

  private String getlogFileName() {
    try {
      if (pro == null) {
        pro = new java.util.Properties();
        fin = getClass().getResourceAsStream("log.properties"); //在类的当前位置,查找属性配置文件log.properties
        pro.load(fin);//载入配置文件
        fin.close();
      }
    }
    catch (IOException ex) {
      System.err.println("无法打开属性配置文件: log.properties" );
      ex.printStackTrace();
    }
    return pro.getProperty("logfile",DefalutLogFilePathName);
    //根据属性值获得日志文件路径,第二个参数是:如果找不到"logfile"标志,就返回的默认值
  }

  /**
   * 你也可以在所有的日志都记录完成的时候,调用该方法,释放资源.
   * free all the resouce,this is secuty method
   */

  public void free() {
    try {
      this.logwriter = null;
      if (out != null){
        this.out.close();
      }
      if (fin != null){
        this.fin.close();
      }
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值