Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29952713
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-03-17 11:43:02

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * 日志操作的实用函数集合
 *
 * @author Quad(百全)
 * @version 1.0
 * @time 2007年09月19日17时06分10秒.656
 * @history 2007年09月19日17时06分10秒.656 新建
 */

 

/**
 * logging.properties示例:
 *
 * handlers = java.util.logging.FileHandler,java.util.logging.ConsoleHandler
 * .Level = ALL
 * java.util.logging.ConsoleHandler.pattern = d:\\logs.log
 * java.util.logging.ConsoleHandler.limit = 50000
 * java.util.logging.ConsoleHandler.count = 2
 * java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter
 * java.util.logging.ConsoleHandler.level = WARNING
 * java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
 */

 

public class LogUtils {

 private LogManager lm = LogManager.getLogManager();

 private Logger log = Logger.getLogger("log");

 private FileInputStream fis = null;

 private FileHandler fh = null;

 /**
  * 读入属性文件,得到日志文件路径
  *
  * @param propertiesFileName
  *            属性文件路径
  * @param logFileName
  *            日志文件路径
  */
 private void init(String propertiesFileName, String logFileName) {

  try {
   // 读入属性文件
   fis = new FileInputStream(new File(propertiesFileName));
  } catch (FileNotFoundException e) {
   System.out.println("Can't find the property file !");
   this.log("Can't find the property file !", null, null, e);
   e.printStackTrace();
  }

  try {
   // 得到日志文件路径
   if (logFileName == null) {
    lm.readConfiguration(fis);
    fh = new FileHandler(lm.getProperty("java.util.logging.ConsoleHandler.pattern"));
   } else {
    lm.readConfiguration(fis);
    fh = new FileHandler(logFileName);
   }
  } catch (SecurityException e) {
   this.log("", null, null, e);
   e.printStackTrace();
  } catch (IOException e) {
   this.log("", null, null, e);
   e.printStackTrace();
  }

  log.addHandler(fh);
  lm.addLogger(log);
 }

 private LogUtils() {
 }

 /**
  * 构造函数
  *
  * @param propertiesFileName
  *            属性文件路径
  * @param logFileName
  *            日志文件路径
  */
 public LogUtils(String propertiesFileName, String logFileName) {
  init(propertiesFileName, logFileName);
 }

 /**
  * 构造函数
  *
  * @param propertiesFileName
  *            属性文件路径
  */
 public LogUtils(String propertiesFileName) {
  init(propertiesFileName, null);
 }

 /**
  * 获取异常信息
  *
  * @param ex
  *            异常
  * @return 异常信息
  */
 private String createExceptionMsg(Throwable ex) {

  String retMsg;

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  PrintStream printStream = new PrintStream(stream);
  ex.printStackTrace(printStream);
  retMsg = stream.toString();

  return retMsg;
 }

 /**
  * 创建日志消息
  *
  * @param msg
  *            自定义消息
  * @param param
  *            参数(用于替换消息中的'?')
  * @param ex
  *            异常
  * @return 日志消息
  */
 private String createMsg(String msg, List param, Throwable ex) {

  String retMsg;
  StringBuffer tempMsg;

  if (msg == null) {
   retMsg = "";
  } else {

   if (msg.indexOf("?") == -1 || param == null) {
    retMsg = msg;
   } else {

    tempMsg = new StringBuffer();
    Iterator iterator = param.iterator();
    int startCount = 0;

    while (iterator.hasNext()) {

     int counter = msg.indexOf("?", startCount);
     tempMsg.append(msg.substring(startCount, counter));

     Object val = iterator.next();

     tempMsg.append(val);
     startCount = counter + 1;
    }

    tempMsg.append(msg.substring(startCount, msg.length()));

    retMsg = tempMsg.toString();
   }

  }

  if (ex != null) {
   retMsg += "\n" + createExceptionMsg(ex);
  }

  return retMsg;
 }

 /**
  * 创建日志消息
  *
  * @param msg
  *            自定义消息
  * @param level
  *            日志的等级(SEVERE > WARNING > INFO)
  * @param param
  *            参数(用于替换消息中的'?')
  * @param ex
  *            异常
  */
 public void log(String msg, String level, List param, Throwable ex) {

  if (level == null) {
   level = "INFO";
  } else {
   level = level.toUpperCase();
  }

  if (param != null) {

   int counter = 0;
   for (int i = 0; i < msg.length(); i++) {
    if (msg.charAt(i) == '?') {
     counter++;
    }
   }
  }

  if (ex == null) {
   msg = createMsg(msg, param, null);
  } else {
   msg = createMsg(msg, param, ex);
  }

  log.log(Level.parse(level), msg);

 }

 /**
  * 创建日志消息
  *
  * @param msg
  *            自定义消息
  */
 public void log(String msg) {
  log(msg, null, null, null);
 }

 /**
  * 创建日志消息
  *
  * @param msg
  *            自定义消息
  * @param level
  *            日志的等级(SEVERE > WARNING > INFO)
  */
 public void log(String msg, String level) {
  log(msg, level, null, null);
 }

 /**
  * 主函数,测试本类
  *
  * @param args
  */
 public static void main(String[] args) {
  LogUtils lu = new LogUtils("D:\\logging.properties");
  List list = new ArrayList();
  list.add("1111");
  list.add("2222");
  list.add("3333");
  
  try {
   lu.log("asdf ? ? ?", "info", list, null);
   list.add("4444");
   lu.log("asdf ? ? ?", "info", list, null);
  } catch (RuntimeException e) {
   lu.log("", null, null, e);
   e.printStackTrace();
  }

 }

}

 

 

/**结果示例:




  2007-09-20T09:10:07
  1190250607281
  0
  log
  INFO
  LogUtils
  log
  10
  asdf 1111 2222 3333


  2007-09-20T09:10:07
  1190250607312
  1
  log
  INFO
  LogUtils
  log
  10
 
java.lang.StringIndexOutOfBoundsException: String index out of range: -11
 at java.lang.String.substring(Unknown Source)
 at LogUtils.createMsg(LogUtils.java:162)
 at LogUtils.log(LogUtils.java:215)
 at LogUtils.main(LogUtils.java:261)


 */

阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~