1 8 package org.apache.avalon.excalibur.logger; 9 10 import java.io.ByteArrayOutputStream ; 11 import java.io.PrintStream ; 12 13 import org.apache.avalon.framework.logger.Logger; 14 15 24 public class BufferedLogger implements Logger { 25 26 private StringBuffer _sb; 27 28 31 32 35 public BufferedLogger() { 36 _sb = new StringBuffer (); 37 } 38 39 42 43 48 public void debug(String message) { 49 append("DEBUG", message); 50 } 51 52 58 public void debug(String message, Throwable throwable) { 59 append("DEBUG", message, throwable); 60 } 61 62 67 public boolean isDebugEnabled() { 68 return true; 69 } 70 71 76 public void info(String message) { 77 append("INFO", message); 78 } 79 80 86 public void info(String message, Throwable throwable) { 87 append("INFO", message, throwable); 88 } 89 90 95 public boolean isInfoEnabled() { 96 return true; 97 } 98 99 104 public void warn(String message) { 105 append("WARN", message); 106 } 107 108 114 public void warn(String message, Throwable throwable) { 115 append("WARN", message, throwable); 116 } 117 118 123 public boolean isWarnEnabled() { 124 return true; 125 } 126 127 132 public void error(String message) { 133 append("ERROR", message); 134 } 135 136 142 public void error(String message, Throwable throwable) { 143 append("ERROR", message, throwable); 144 } 145 146 151 public boolean isErrorEnabled() { 152 return true; 153 } 154 155 160 public void fatalError(String message) { 161 append("FATAL ERROR", message); 162 } 163 164 170 public void fatalError(String message, Throwable throwable) { 171 append("FATAL ERROR", message, throwable); 172 } 173 174 179 public boolean isFatalErrorEnabled() { 180 return true; 181 } 182 183 190 public Logger getChildLogger(String name) { 191 return this; 192 } 193 194 197 private void append(String message) { 198 199 synchronized (_sb) { 200 _sb.append(message); 201 _sb.append("\n"); 202 } 203 } 204 205 private void append(String level, String message) { 206 207 synchronized (_sb) { 208 _sb.append(level); 209 _sb.append(" - "); 210 _sb.append(message); 211 _sb.append("\n"); 212 } 213 } 214 215 private void append(String level, String message, Throwable throwable) { 216 217 synchronized (_sb) { 218 String tDump = null; 219 ByteArrayOutputStream ba = new ByteArrayOutputStream (); 220 PrintStream ps = new PrintStream (ba); 221 222 try { 223 throwable.printStackTrace(ps); 224 225 tDump = ba.toString(); 226 } finally { 227 ps.close(); 228 } 229 230 _sb.append(level); 231 _sb.append(" - "); 232 _sb.append(message); 233 _sb.append(" : "); 234 _sb.append(tDump); 235 _sb.append("\n"); 236 } 237 } 238 239 245 public String toString() { 246 return _sb.toString(); 247 } 248 } 249 | Popular Tags |