1 package org.enhydra.snapper; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.File ; 5 import java.io.PrintStream ; 6 import java.io.RandomAccessFile ; 7 import java.util.GregorianCalendar ; 8 9 import com.lutris.appserver.server.Enhydra; 10 import com.lutris.logging.Logger; 11 12 15 public class Log { 16 17 20 private static boolean disabled = false; 21 22 private static String getLogFile() { 23 return ('/'==File.separatorChar ?"/tmp":"")+"/snapper.log"; 24 } 25 26 30 public static void logToFile(String message) { 31 Enhydra.getLogChannel().write(Logger.INFO, message); 32 } 34 35 public static void log(String message) { 36 Enhydra.getLogChannel().write(Logger.INFO, message); 39 } 40 41 46 public static void logToFile(String fileLog, String message) { 47 try { 48 if (!disabled) { 49 File file = new File (fileLog); 50 if (!file.exists()) { 51 file.createNewFile(); 52 } 53 RandomAccessFile fileLogr = new RandomAccessFile (fileLog, "rw"); 54 fileLogr.seek(fileLogr.length()); 55 fileLogr.writeBytes(getTotalTime() + message + "\r\n"); 56 fileLogr.close(); 57 } 58 } 59 catch (Exception ex) { 60 ex.printStackTrace(); 61 } 62 } 63 64 public static void logException(Throwable e) { 65 if(!disabled) { 66 e.printStackTrace(); 67 ByteArrayOutputStream out = new ByteArrayOutputStream (); 68 e.printStackTrace(new PrintStream (out)); 69 Enhydra.getLogChannel().write(Logger.CRITICAL, out.toString()); 70 } 72 } 73 74 private static String getTotalTime() { 75 StringBuffer time = new StringBuffer (); 76 time.append("["); 77 GregorianCalendar cal = new GregorianCalendar (); 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 |