1 24 25 package com.mckoi.util; 26 27 import java.io.*; 28 import java.text.*; 29 import java.util.Date ; 30 31 38 39 public class Log { 40 41 44 private final LogWriter log_output; 45 46 49 private final DateFormat date_format = DateFormat.getDateTimeInstance(); 50 51 52 public Log(String path) throws FileNotFoundException, IOException { 53 this(new File(path)); 54 } 55 56 public Log(File file, int size, int max_count) throws IOException { 57 this.log_output = new LogWriter(file, size, max_count); 58 } 59 60 public Log(File file) throws FileNotFoundException, IOException { 61 this(file, 512 * 1024, 12); 63 } 65 66 protected Log() { 67 log_output = null; 68 } 69 70 74 public synchronized void log(String text) { 75 try { 76 log_output.write("["); 77 log_output.write(date_format.format(new Date ())); 78 log_output.write("] "); 79 log_output.write(text); 80 log_output.flush(); 81 } 82 catch (IOException e) {} 83 } 84 85 public synchronized void logln(String text) { 86 try { 87 log_output.write(text); 88 log_output.write('\n'); 89 log_output.flush(); 90 } 91 catch (IOException e) {} 92 } 93 94 97 public synchronized void close() { 98 try { 99 log_output.close(); 100 } 101 catch (IOException e) {} 102 } 103 104 106 110 public static Log nullLog() { 111 return new NullLog(); 112 } 113 114 116 } 117 118 121 class NullLog extends Log { 122 123 public NullLog() { 124 super(); 125 } 126 127 public void log(String text) { 128 } 130 public void logln(String text) { 131 } 133 public void close() { 134 } 136 137 } 138 139 | Popular Tags |