import java.io.*;
import java.util.*;
/** Title: 静态日志操作类 Description: Copyright: Copyright (c) 2004 Company:
?* 类的具体使用:
?*
?* 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封装一下,实现静态方便的调用.
?*
?*
?*
?*
?*
?* @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();
??? }
? }
}
该博客介绍了Java中静态日志操作类LogWriter的实现与使用。包括编译类、创建属性配置文件,以及使用静态方法记录日志信息和异常信息。还说明了相关方法调用的注意事项,如getClass().getResourceAsStream不支持static调用的处理,也提及可封装log4J实现静态调用。

3163

被折叠的 条评论
为什么被折叠?



