1import java.io.*;
  2import java.util.*;
  3/**
  4  *
  5  * <p>Title: LOG 日志记录</p>
  6  * <p>Description:
  7  * 此类主要用来记录系统中发生的重大事件,以及由于程序本身所产生的错误信息</p>
  8  * <p>Copyright: Copyright (c) 2003</p>
  9  * <p>Company: hoten </p>
 10  * @author lqf
 11  * @version 1.0
 12  */

 13////////////////////////////////////////////////////////////////////////////////////////////
 14//
 15///////////////////////////////////////////////////////////////////////////////////////////
 16public class Log{
 17  /**
 18   * 用来记录系统重大事件
 19   * @param msg 重大事件
 20   * @param fileName 日志文件的路径及名称
 21   */

 22
 23    public static void printBytes(byte[] msg,String logFile){
 24         StringBuffer sb = new StringBuffer(100);
 25         for(int i=0;i<msg.length;i++){
 26             sb.append((int)msg[i]);
 27             sb.append(",");
 28         }

 29         printEvent(sb.toString(),logFile);
 30   }

 31  public synchronized static void printEvent(String msg,String fileName)
 32   {
 33      msg = new String( "时间:"+CTime.getTime(CTime.YYMMDDhhmmss) + " 事件消息:  " + msg);
 34      if(fileName!=null) printToFile(msg,fileName);
 35      else print(msg);
 36      return;
 37   }

 38
 39   public synchronized static void printError(Throwable e,String msg,String fileName)
 40   {
 41      StringBuffer errors=new StringBuffer(100);
 42      errors.append("时间:");
 43      errors.append(CTime.getTime(CTime.YYMMDDhhmmssxxx));
 44      errors.append(" 消息:");
 45      errors.append(msg);
 46      errors.append(" Exception: ");
 47      if(fileName!=null{
 48            printToFile(errors.toString().trim(),fileName);
 49            try {
 50                e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));//
 51            }

 52            catch (Exception ex) {
 53            }

 54      }

 55      else print(errors.toString().trim());
 56      return;
 57   }

 58/**
 59 * 记录应程序本身发生的错误,主要给程序员观察。
 60 * @param e  一个Exception
 61 * @param mobile 用户手机号码
 62 * @param msg   用户发送的消息
 63 * @param fileName 日志文件的路径及名称
 64 */

 65   public synchronized static void printError(Throwable e,String mobile,String msg,String fileName)
 66   {
 67      StringBuffer errors=new StringBuffer(100);
 68      errors.append("时间:");
 69      errors.append(CTime.getTime(CTime.YYMMDDhhmmssxxx));
 70      errors.append(" 手机号码:");
 71      errors.append(mobile);
 72      errors.append(" 消息:");
 73      errors.append(msg);
 74      errors.append(" Exception: ");
 75      if(fileName!=null{
 76            printToFile(errors.toString().trim(),fileName);
 77            try {
 78                e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));//
 79            }

 80            catch (Exception ex) {
 81            }

 82      }

 83      else print(errors.toString().trim());
 84      return;
 85   }

 86/**把错误消息打印到屏幕上
 87 *
 88 * @param msg 错误消息
 89 */

 90   private static void print(String msg)
 91   {
 92       System.out.println(msg);
 93   }

 94/**
 95 * 把消息打印到指定文件
 96 * @param msg 错误消息
 97 * @param fileName 指定的文件
 98 */

 99
100   private static void printToFile(String msg,String fileName) //打印到文件中
101   {
102      BufferedWriter mBufWriter = null;
103      try
104      {
105         if(!createFile(fileName)) return ;
106         FileWriter fileWriter = new FileWriter(fileName, true);
107         mBufWriter = new BufferedWriter(fileWriter);
108
109         mBufWriter.write(msg);
110         mBufWriter.newLine();
111
112         mBufWriter.flush();
113         mBufWriter.close();
114      }

115      catch (Throwable e)
116      {
117         try { mBufWriter.close(); } catch (Throwable t) {};
118      }

119      return;
120   }

121/**
122 * 用来创建文件和文件夹
123 * @param fileName 文件或文件夹名称
124 * @return
125 * @throws IOException 
126 * @throws Exception
127 */

128
129  private static boolean createFile(String fileName)throws IOException ,Exception{
130        File file = new File(fileName);
131         if (file.exists()) /* does file exist? If so, can it be written to */
132         {
133            if (file.canWrite() == false)
134               return false;
135         }

136         else
137         {
138           String path = null;  /* Does not exist.  Create the directories */
139
140           int firstSlash = fileName.indexOf(File.separatorChar);
141           int finalSlash = fileName.lastIndexOf(File.separatorChar);
142
143           if (finalSlash == 0/* error, not valid path */ }
144           else if (finalSlash == 1/* UNIX root dir */
145           {
146             path = File.separator;
147           }

148           else if (firstSlash == finalSlash)
149           /* for example c:\  Then make sure slash is part of path */
150             path = fileName.substring(0,finalSlash+1);
151           }

152           else
153           { path = fileName.substring(0,finalSlash); }
154
155           File dir = new File(path);
156           dir.mkdirs();
157         }

158         return true;
159  }

160}