站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题编写一个随处可调用的静态日志操作类

编写一个随处可调用的静态日志操作类

摘要: 编写一个随处可调用的静态日志操作类

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="282" height="86" align="center" valign="top"> </td> <td width="402" valign="top">

编写一个随处可调用的静态日志操作类

日志文件是一个随处都在使用的文件,它可以很好的记录程序的运行状态和出错信息,几乎每一个 程序都有它的日志文件,在我们用java开发web项目中,会经常需要记录用户的一些登陆,访问, 操作信息.如:一个办公系统,它的日志文件需要记录:
………
Wed May 19 13:35:50 CST 2004:张三登陆了该系统
Wed May 19 13:35:53 CST 2004:张三插入了一条"下周计划"记录 id:1048
Wed May 19 13:35:54 CST 2004:李四登陆了该系统
Wed May 19 13:35:55 CST 2004:张三删除了一条"上周总结"记录, d:1024
Wed May 19 13:35:59 CST 2004:张三退出了该系统
................

</td> </tr> </table>

实现思路:
1. 为了很好的实现这个记录类,必须要使用”单态”模式,这样该类不必每次调用的时候都需要生成它的实例.
2. 为了防止多线程同时操作(写)日志文件,造成文件”死锁”,必须考虑同步,使用synchronized关键字.
3. 为了不必关心该类唯一实例的生成,而直接使用该类的静态方法实现日志的记录
4. 为了更方便的配置日志文件的路径,使用属性文件配置.

废话太多了,不像搞程序的了,直接看代码,所有的注释在代码中说明:

import java.io.*;

import java.util.*;

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 message infomation

*/

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();

  }

 }

} 

类的具体使用::
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封装一下,实现静态方便的调用.

三.我同时使用多个线程测试过该类.没有发现问题.因为对java才研究了一年.如果高手们
看出其中的错误或者不足之处,也请多多赐教.谢谢.具体问题可联:qq:29189725



</td> </tr> <tr>


↑返回目录
前一篇: 揭开AJAX神秘面纱
后一篇: 使用JSP表达式语言定制缓存内容。