KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > Log


1 package org.enhydra.snapper;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.PrintStream JavaDoc;
6 import java.io.RandomAccessFile JavaDoc;
7 import java.util.GregorianCalendar JavaDoc;
8
9 import com.lutris.appserver.server.Enhydra;
10 import com.lutris.logging.Logger;
11
12 /**
13  * Class used for logging messages to file.
14  */

15 public class Log {
16
17   /**
18    * When set to 'true' , this class will not log into file.
19    */

20   private static boolean disabled = false;
21
22   private static String JavaDoc getLogFile() {
23       return ('/'==File.separatorChar ?"/tmp":"")+"/snapper.log";
24   }
25
26   /**
27    * Logs message to default file.
28    * @param message message for logging,
29    */

30   public static void logToFile(String JavaDoc message) {
31     Enhydra.getLogChannel().write(Logger.INFO, message);
32     //logToFile(getLogFile(), message);
33
}
34
35   public static void log(String JavaDoc message) {
36       //logToFile(getLogFile(),getTotalTime() + message + "\r\n");
37
//System.out.print(getTotalTime() + message + "\r\n");
38
Enhydra.getLogChannel().write(Logger.INFO, message);
39   }
40   
41   /**
42    * Logs message to file.
43    * @param fileLog file path and name which is used for logging.
44    * @param message message for logging,
45    */

46   public static void logToFile(String JavaDoc fileLog, String JavaDoc message) {
47     try {
48       if (!disabled) {
49         File JavaDoc file = new File JavaDoc(fileLog);
50         if (!file.exists()) {
51           file.createNewFile();
52         }
53         RandomAccessFile JavaDoc fileLogr = new RandomAccessFile JavaDoc(fileLog, "rw");
54         fileLogr.seek(fileLogr.length());
55         fileLogr.writeBytes(getTotalTime() + message + "\r\n");
56         fileLogr.close();
57       }
58     }
59     catch (Exception JavaDoc ex) {
60       ex.printStackTrace();
61     }
62   }
63
64   public static void logException(Throwable JavaDoc e) {
65         if(!disabled) {
66             e.printStackTrace();
67             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
68             e.printStackTrace(new PrintStream JavaDoc(out));
69             Enhydra.getLogChannel().write(Logger.CRITICAL, out.toString());
70             //logToFile(out.toString());
71
}
72     }
73   
74   private static String JavaDoc getTotalTime() {
75     StringBuffer JavaDoc time = new StringBuffer JavaDoc();
76     time.append("[");
77     GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
78     time.append(cal.get(GregorianCalendar.HOUR_OF_DAY));
79     time.append(":");
80     time.append(cal.get(GregorianCalendar.MINUTE));
81     time.append(":");
82     time.append(cal.get(GregorianCalendar.SECOND));
83     time.append("]");
84     time.append(" ");
85     return time.toString();
86   }
87 }
88
Popular Tags