1 46 47 package org.mr.core.log; 48 49 import java.util.Collection ; 50 import java.util.Iterator ; 51 import java.util.LinkedList ; 52 53 import org.apache.commons.logging.LogFactory; 54 55 72 public class StartupLogger { 73 74 private static final int TRACE = 0; 76 private static final int DEBUG = 1; 77 private static final int INFO = 2; 78 private static final int WARN = 3; 79 private static final int ERROR = 4; 80 private static final int FATAL = 5; 81 82 private class StartupLoggerEntry { 84 String msg; 85 String source; 86 int logLevel; 87 88 StartupLoggerEntry(String msg, String source, int logLevel) { 89 this.msg = msg; 90 this.source = source; 91 this.logLevel = logLevel; 92 } 93 94 public String toString() { 95 return msg; 96 } 97 } 98 99 public static StartupLogger log = new StartupLogger(); 101 102 private boolean hasErrors = false; 104 105 private boolean hasFatals = false; 107 108 private boolean hasWarnings = false; 110 111 private Collection messages = new LinkedList (); 113 114 private boolean store = false; 116 117 interface Sender { 119 public void send(String msg, String loggerName); 120 } 121 122 private Sender[] senders; 124 125 private StartupLogger() { 127 initSenders(); 128 } 129 130 private void initSenders() { 132 senders = new Sender[6]; 133 senders[TRACE] = new Sender() { 134 public void send(String msg, String source) { 135 LogFactory.getLog(source).trace(msg); 136 } 137 }; 138 senders[DEBUG] = new Sender() { 139 public void send(String msg, String source) { 140 LogFactory.getLog(source).debug(msg); 141 } 142 }; 143 senders[INFO] = new Sender() { 144 public void send(String msg, String source) { 145 LogFactory.getLog(source).info(msg); 146 } 147 }; 148 senders[WARN] = new Sender() { 149 public void send(String msg, String source) { 150 LogFactory.getLog(source).warn(msg); 151 } 152 }; 153 senders[ERROR] = new Sender() { 154 public void send(String msg, String source) { 155 LogFactory.getLog(source).error(msg); 156 } 157 }; 158 senders[FATAL] = new Sender() { 159 public void send(String msg, String source) { 160 LogFactory.getLog(source).fatal(msg); 161 } 162 }; 163 } 164 165 public void startStore() { 167 this.store = true; 168 } 169 170 private synchronized void log(String msg, String source, int logLevel) { 172 messages.add(new StartupLoggerEntry(msg, source, logLevel)); 173 } 174 175 public synchronized void trace(String msg, String source) { 177 if (!store) { 178 LogFactory.getLog(source).trace(msg); 179 return; 180 } 181 messages.add(new StartupLoggerEntry(msg, source, TRACE)); 182 } 183 184 public synchronized void debug(String msg, String source) { 186 if (!store) { 187 LogFactory.getLog(source).debug(msg); 188 return; 189 } 190 messages.add(new StartupLoggerEntry(msg, source, DEBUG)); 191 } 192 193 public synchronized void info(String msg, String source) { 195 if (!store) { 196 LogFactory.getLog(source).info(msg); 197 return; 198 } 199 messages.add(new StartupLoggerEntry(msg, source, INFO)); 200 } 201 202 public synchronized void warn(String msg, String source) { 204 if (!store) { 205 LogFactory.getLog(source).warn(msg); 206 return; 207 } 208 messages.add(new StartupLoggerEntry(msg, source, WARN)); 209 hasWarnings = true; 210 } 211 212 public synchronized void error(String msg, String source) { 214 if (!store) { 215 LogFactory.getLog(source).error(msg); 216 return; 217 } 218 messages.add(new StartupLoggerEntry(msg, source, ERROR)); 219 hasErrors = true; 220 } 221 222 public synchronized void fatal(String msg, String source) { 224 if (!store) { 225 LogFactory.getLog(source).fatal(msg); 226 return; 227 } 228 messages.add(new StartupLoggerEntry(msg, source, FATAL)); 229 hasFatals = true; 230 } 231 232 public boolean hasErrors() { 234 return hasErrors; 235 } 236 237 public boolean hasFatals() { 239 return hasFatals; 240 } 241 242 public boolean hasWarnings() { 244 return hasWarnings; 245 } 246 247 private void printTrace() { 249 try {throw new Exception ();} 250 catch (Exception e) {e.printStackTrace();} 251 } 252 253 public synchronized void flush() { 255 if (!store) { 256 printTrace(); 257 return; 258 } 259 store = false; 260 StartupLoggerEntry entry; 261 Iterator entries = messages.iterator(); 262 int size = messages.size(); 263 for (int i=0 ; i<size ; i++) { 264 entry = (StartupLoggerEntry)entries.next(); 265 senders[entry.logLevel].send(entry.toString(), entry.source); 266 } 267 messages.clear(); 268 } 269 } 270 | Popular Tags |