1 7 package winstone; 8 9 import java.io.OutputStream ; 10 import java.io.PrintWriter ; 11 import java.io.StringWriter ; 12 import java.text.DateFormat ; 13 import java.text.SimpleDateFormat ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Date ; 17 import java.util.Hashtable ; 18 import java.util.Map ; 19 20 28 public class Logger { 29 public final static String DEFAULT_STREAM = "Winstone"; 30 public static int MIN = 1; 31 public static int ERROR = 2; 32 public static int WARNING = 3; 33 public static int INFO = 5; 34 public static int SPEED = 6; 35 public static int DEBUG = 7; 36 public static int FULL_DEBUG = 8; 37 public static int MAX = 9; 38 39 protected static Boolean semaphore = new Boolean (true); 40 protected static boolean initialised = false; 41 protected static Map streams; 42 protected static Collection nullStreams; 43 protected static int currentDebugLevel; 44 protected final static DateFormat sdfLog = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 45 protected static boolean showThrowingThread; 47 48 51 public static void init(int level) { 52 init(level, System.out, false); 53 } 55 56 59 public static void init(int level, OutputStream defaultStream, 60 boolean showThrowingThreadArg) { 62 currentDebugLevel = level; 63 streams = new Hashtable (); 64 nullStreams = new ArrayList (); 65 initialised = true; 66 setStream(DEFAULT_STREAM, defaultStream); 67 showThrowingThread = showThrowingThreadArg; 69 } 70 71 74 public static void setStream(String name, OutputStream stream) { 75 if (stream == null) 76 nullStreams.add(name); 77 else 78 setStream(name, new PrintWriter (stream)); 79 } 80 81 84 public static void setStream(String name, PrintWriter stream) { 85 { 87 if (!initialised) 88 init(INFO); 89 if (stream == null) 90 nullStreams.add(name); 91 else 92 streams.put(name, stream); 93 } 94 } 95 96 99 public static void flush(String name) { 100 { 102 if (!initialised) 103 init(INFO); 104 PrintWriter p = (PrintWriter ) streams.get(name); 105 if (p != null) 106 p.flush(); 107 } 108 } 109 110 public static void setCurrentDebugLevel(int level) { 111 if (!initialised) 112 init(level); 113 else 114 currentDebugLevel = level; 115 } 116 117 132 private static void logInternal(String streamName, String message, Throwable error) { 133 134 if (!initialised) { 135 init(INFO); 136 } 137 138 String lineNoText = ""; 139 if (showThrowingThread) { 150 lineNoText += "[" + Thread.currentThread().getName() + "] - "; 151 } 152 153 PrintWriter stream = (PrintWriter ) streams.get(streamName); 154 boolean nullStream = nullStreams.contains(streamName); 155 if ((stream == null) && !nullStream) 156 stream = (PrintWriter ) streams.get(DEFAULT_STREAM); 157 158 if (stream != null) { 159 StringBuffer fullMessage = new StringBuffer (); 160 String date = null; 161 synchronized (sdfLog) { 162 date = sdfLog.format(new Date ()); 163 } 164 fullMessage.append("[").append(streamName).append(" ").append( 165 date).append("] - ").append(lineNoText).append(message); 166 if (error != null) { 167 StringWriter sw = new StringWriter (); 168 PrintWriter pw = new PrintWriter (sw); 169 error.printStackTrace(pw); 170 pw.flush(); 171 fullMessage.append('\n').append(sw.toString()); 172 } 173 stream.println(fullMessage.toString()); 174 stream.flush(); 175 } 176 } 177 178 public static void log(int level, WinstoneResourceBundle resources, 179 String messageKey) { 180 if (currentDebugLevel < level) 181 return; 182 else 183 logInternal(DEFAULT_STREAM, resources.getString(messageKey), null); 184 } 185 186 public static void log(int level, WinstoneResourceBundle resources, 187 String messageKey, Throwable error) { 188 if (currentDebugLevel < level) 189 return; 190 else 191 logInternal(DEFAULT_STREAM, resources.getString(messageKey), error); 192 } 193 194 public static void log(int level, WinstoneResourceBundle resources, 195 String messageKey, String param) { 196 if (currentDebugLevel < level) 197 return; 198 else 199 logInternal(DEFAULT_STREAM, resources.getString(messageKey, param), null); 200 } 201 202 public static void log(int level, WinstoneResourceBundle resources, 203 String messageKey, String params[]) { 204 if (currentDebugLevel < level) 205 return; 206 else 207 logInternal(DEFAULT_STREAM, resources.getString(messageKey, params), null); 208 } 209 210 public static void log(int level, WinstoneResourceBundle resources, 211 String messageKey, String param, Throwable error) { 212 if (currentDebugLevel < level) 213 return; 214 else 215 logInternal(DEFAULT_STREAM, resources.getString(messageKey, param), error); 216 } 217 218 public static void log(int level, WinstoneResourceBundle resources, 219 String messageKey, String params[], Throwable error) { 220 if (currentDebugLevel < level) 221 return; 222 else 223 logInternal(DEFAULT_STREAM, resources.getString(messageKey, params), error); 224 } 225 226 public static void log(int level, WinstoneResourceBundle resources, 227 String streamName, String messageKey, String params[], Throwable error) { 228 if (currentDebugLevel < level) 229 return; 230 else 231 logInternal(streamName, resources.getString(messageKey, params), error); 232 } 233 234 public static void logDirectMessage(int level, String streamName, String message, 235 Throwable error) { 236 if (currentDebugLevel < level) 237 return; 238 else 239 logInternal(streamName, message, error); 240 } 241 } 242 | Popular Tags |