01: package org.enhydra.snapperAdmin;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.File;
05: import java.io.PrintStream;
06: import java.io.RandomAccessFile;
07: import java.util.GregorianCalendar;
08:
09: 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 getLogFile() {
23: return ('/' == File.separatorChar ? "/tmp" : "")
24: + "/snapper.log";
25: }
26:
27: /**
28: * Logs message to default file.
29: * @param message message for logging,
30: */
31: public static void logToFile(String message) {
32: Enhydra.getLogChannel().write(Logger.INFO, message);
33: //logToFile(getLogFile(), message);
34: }
35:
36: public static void log(String message) {
37: //logToFile(getLogFile(),getTotalTime() + message + "\r\n");
38: //System.out.print(getTotalTime() + message + "\r\n");
39: Enhydra.getLogChannel().write(Logger.INFO, message);
40: }
41:
42: /**
43: * Logs message to file.
44: * @param fileLog file path and name which is used for logging.
45: * @param message message for logging,
46: */
47: public static void logToFile(String fileLog, String message) {
48: try {
49: if (!disabled) {
50: File file = new File(fileLog);
51: if (!file.exists()) {
52: file.createNewFile();
53: }
54: RandomAccessFile fileLogr = new RandomAccessFile(
55: fileLog, "rw");
56: fileLogr.seek(fileLogr.length());
57: fileLogr.writeBytes(getTotalTime() + message + "\r\n");
58: fileLogr.close();
59: }
60: } catch (Exception ex) {
61: ex.printStackTrace();
62: }
63: }
64:
65: public static void logException(Throwable e) {
66: if (!disabled) {
67: e.printStackTrace();
68: ByteArrayOutputStream out = new ByteArrayOutputStream();
69: e.printStackTrace(new PrintStream(out));
70: Enhydra.getLogChannel().write(Logger.CRITICAL,
71: out.toString());
72: //logToFile(out.toString());
73: }
74: }
75:
76: private static String getTotalTime() {
77: StringBuffer time = new StringBuffer();
78: time.append("[");
79: GregorianCalendar cal = new GregorianCalendar();
80: time.append(cal.get(GregorianCalendar.HOUR_OF_DAY));
81: time.append(":");
82: time.append(cal.get(GregorianCalendar.MINUTE));
83: time.append(":");
84: time.append(cal.get(GregorianCalendar.SECOND));
85: time.append("]");
86: time.append(" ");
87: return time.toString();
88: }
89: }
|