1 package com.protomatter.syslog; 2 3 52 53 import java.io.PrintWriter ; 54 import java.util.*; 55 import java.text.*; 56 import java.net.*; 57 import java.io.*; 58 import java.lang.reflect.*; 59 60 import com.protomatter.util.*; 61 62 69 public class SimpleSyslogTextFormatter 70 implements SyslogTextFormatter 71 { 72 private static String crString = System.getProperty("line.separator"); 73 private static char[] cr = System.getProperty("line.separator").toCharArray(); 74 private static char[] rb = " [".toCharArray(); 75 private static char[] lb = "] ".toCharArray(); 76 private static char rb_ns = '['; 77 private static char[] sp = " ".toCharArray(); 78 private static char[] parens = "()".toCharArray(); 79 private static char sp_1 = ' '; 80 private static char dot = '.'; 81 private static char colon = ':'; 82 83 private static char[] DEBUG = "DBUG".toCharArray(); 84 private static char[] INFO = "INFO".toCharArray(); 85 private static char[] WARNING = "WARN".toCharArray(); 86 private static char[] ERROR = "EROR".toCharArray(); 87 private static char[] FATAL = "FTAL".toCharArray(); 88 private static char[] UNKNOWN_LEVEL = "????".toCharArray(); 89 90 private static String CH_ALL_CHANNEL = "ALL_CHANNEL"; 91 private static String CH_DEF_CHANNEL = "DEFAULT_CHANNEL"; 92 93 private DateFormat dateFormat = null; 94 private TimeZone dateFormatTimeZone = TimeZone.getDefault(); 95 private String dateFormatString = null; 96 private long lastDate = -1; 97 private char[] lastDateString = null; 98 private int dateFormatCacheTime = 1000; 99 private boolean showCaller = true; 100 private boolean showChannel = false; 101 private boolean showThreadName = false; 102 private boolean showHostName = false; 103 104 private int classWidth = 20; 105 private int hostWidth = 15; 106 private int channelWidth = 15; 107 private int threadWidth = 15; 108 109 112 public SimpleSyslogTextFormatter() 113 { 114 super(); 115 setDateFormat("MM/dd/yyyy HH:mm:ss"); 116 } 117 118 121 public void formatLogEntry(StringBuffer b, SyslogMessage message) 122 { 123 synchronized (b) 124 { 125 b.append(formatDate(message.time)); 126 b.append(rb); 127 b.append(getStringForLevel(message.level)); 128 b.append(lb); 129 130 if (showChannel) 131 { 132 if (message.channel.equals(Syslog.ALL_CHANNEL)) 133 { 134 b.append(rb_ns); 135 justify(b, CH_ALL_CHANNEL, getChannelWidth()); 136 b.append(lb); 137 } 138 else if (message.channel.equals(Syslog.DEFAULT_CHANNEL)) 139 { 140 b.append(rb_ns); 141 justify(b, CH_DEF_CHANNEL, getChannelWidth()); 142 b.append(lb); 143 } 144 else 145 { 146 b.append(rb_ns); 147 justify(b, message.channel, getChannelWidth()); 148 b.append(lb); 149 } 150 } 151 152 if (showHostName) 153 { 154 justify(b, getHostname(message.host), getHostWidth()); 155 b.append(sp_1); 156 } 157 158 if (showThreadName) 159 { 160 b.append(rb_ns); 161 b.append("T:"); 162 justify(b, message.threadName, getThreadWidth()); 163 b.append(lb); 164 } 165 if (showCaller) 166 { 167 formatLoggerClassName(b, message); 168 } 169 170 if (message.msg != null) 171 { 172 b.append(sp); 173 b.append(message.msg); 174 } 175 b.append(cr); 176 if (message.detail != null) 177 { 178 formatMessageDetail(b, message); 179 } 180 } 181 } 182 183 public void formatLoggerClassName(StringBuffer b, SyslogMessage message) 184 { 185 trimFromLastPeriod(b, message.loggerClassname, message.callingMethodName, 186 message.callingMethodLineNumber, getClassWidth()); 187 } 188 189 public void formatMessageDetail(StringBuffer b, SyslogMessage message) 190 { 191 if (message.detail == null) 192 return; 193 194 String temp = null; 195 196 if (message.detail instanceof Throwable ) 198 { 199 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 200 PrintWriter pw = new PrintWriter (bs); 201 Throwable e = (Throwable )message.detail; 202 Throwable e2 = e; 203 Object [] junk = null; 204 String methodCalled = null; 205 while (e2 != null) 206 { 207 if (methodCalled != null) 208 { 209 pw.print(methodCalled); 210 pw.print("(): "); 211 } 212 e2.printStackTrace(pw); 213 junk = getNextException(e2); 214 e2 = (Throwable )junk[1]; 215 methodCalled = (String )junk[0]; 216 } 217 pw.flush(); 218 temp = bs.toString(); 219 b.append(temp); 220 if (!temp.endsWith(crString)) 221 b.append(cr); 222 } 223 else 224 { 225 temp = message.detail.toString(); 226 b.append(temp); 227 if (!temp.endsWith(crString)) 228 b.append(cr); 229 } 230 } 231 232 242 protected Object [] getNextException(Throwable t) 243 { 244 if (t == null) 245 return null; 246 Method methods[] = t.getClass().getMethods(); 247 Class pt[] = null; 248 Class rt = null; 249 boolean isFIST = false; 250 String name = null; 251 Object junk[] = new Object [2]; 252 253 for (int i=0; i<methods.length; i++) 254 { 255 rt = methods[i].getReturnType(); 256 pt = methods[i].getParameterTypes(); 257 name = methods[i].getName(); 258 isFIST = name.equals("fillInStackTrace"); if (!isFIST && pt.length == 0 && Throwable .class.isAssignableFrom(rt)) 260 { 261 try 262 { 263 junk[0] = name; 264 junk[1] = (Throwable )methods[i].invoke(t, new Object [0]); 265 return junk; 266 } 267 catch (Throwable x) 268 { 269 return junk; 271 } 272 } 273 } 274 return junk; 275 } 276 277 protected char[] getStringForLevel(int level) 278 { 279 switch (level) 280 { 281 case Syslog.DEBUG: return DEBUG; 282 case Syslog.INFO: return INFO; 283 case Syslog.WARNING: return WARNING; 284 case Syslog.ERROR: return ERROR; 285 case Syslog.FATAL: return FATAL; 286 default: return UNKNOWN_LEVEL; 287 } 288 } 289 290 public String getHostname(InetAddress host) 291 { 292 if (host == null) 293 return "<null>"; 294 295 String ip = host.getHostAddress(); 296 String name = host.getHostName(); 297 if (ip.equals(name)) 298 return ip; 299 300 int idx = name.indexOf("."); 301 if (idx == -1) 302 return name; 303 return name.substring(0, idx); 304 } 305 306 309 public void setDateFormat(String format) 310 { 311 this.dateFormatString = format; 312 this.dateFormat = new SimpleDateFormat(format); 313 setDateFormatTimezone(TimeZone.getDefault()); 314 resetDateFormat(); 315 } 316 317 320 public String getDateFormat() 321 { 322 return this.dateFormatString; 323 } 324 325 331 public void setShowHostName(boolean showHostName) 332 { 333 this.showHostName = showHostName; 334 if (showHostName && (Syslog.getLocalHostName() == null)) 335 { 336 Syslog.setLocalHostName(); 337 } 338 } 339 340 343 public boolean getShowHostName() 344 { 345 return this.showHostName; 346 } 347 348 351 public void setShowThreadName(boolean showThreadName) 352 { 353 this.showThreadName = showThreadName; 354 } 355 356 359 public boolean getShowThreadName() 360 { 361 return this.showThreadName; 362 } 363 364 367 public void setShowCaller(boolean showCaller) 368 { 369 this.showCaller = showCaller; 370 } 371 372 375 public boolean getShowCaller() 376 { 377 return this.showCaller; 378 } 379 380 383 public void setShowChannel(boolean showChannel) 384 { 385 this.showChannel = showChannel; 386 } 387 388 391 public boolean getShowChannel() 392 { 393 return this.showChannel; 394 } 395 396 401 public void setDateFormatCacheTime(int cacheTime) 402 { 403 this.dateFormatCacheTime = cacheTime; 404 } 405 406 411 public int getDateFormatCacheTime() 412 { 413 return this.dateFormatCacheTime; 414 } 415 416 420 public void setDateFormatTimezone(TimeZone zone) 421 { 422 this.dateFormatTimeZone = zone; 423 this.dateFormat.setTimeZone(zone); 424 } 425 426 429 public TimeZone getDateFormatTimezone() 430 { 431 return this.dateFormatTimeZone; 432 } 433 434 441 protected char[] formatDate(long theDate) 442 { 443 if (lastDate == -1 || theDate > lastDate+dateFormatCacheTime) 446 { 447 synchronized (dateFormat) 448 { 449 if (lastDate == -1 || theDate > lastDate+dateFormatCacheTime) 451 { 452 lastDateString = dateFormat.format(new Date(theDate)).toCharArray(); 453 lastDate = theDate; 454 } 455 } 456 } 457 return lastDateString; 458 } 459 460 463 protected void trimFromLastPeriod(StringBuffer b, String s, int width) 464 { 465 trimFromLastPeriod(b, s, null, StackTraceInfo.LINE_NUMBER_UNKNOWN, width); 466 } 467 468 471 protected void trimFromLastPeriod(StringBuffer b, String className, String method, int line, int width) 472 { 473 char data[] = (className == null) ? new char[0] : className.toCharArray(); 474 475 int i=data.length; 476 for (; --i>=0 && data[i] != '.';); 477 478 i++; 479 int len = data.length - i; 480 b.append(data, i, len); 481 len = width - len; 482 483 if (method != null) 484 { 485 b.append(dot); 486 b.append(method); 487 b.append(parens); 488 len -= 3; len -= method.length(); 490 if (line != StackTraceInfo.LINE_NUMBER_UNKNOWN) 491 { 492 b.append(colon); 493 String lineString = String.valueOf(line); 494 b.append(lineString); 495 len--; 496 len -= lineString.length(); 497 } 498 } 499 500 for (;--len>=0;) 501 b.append(sp_1); 502 } 503 504 507 protected String trimFromLastPeriod(String s) 508 { 509 int pos = s.lastIndexOf('.'); 510 if (pos >= 0) 511 return s.substring(pos+1); 512 else 513 return s; 514 } 515 516 521 public void resetDateFormat() 522 { 523 this.lastDate = -1; 524 } 525 526 529 public String getLogHeader() 530 { 531 return ""; 532 } 533 534 537 public String getLogFooter() 538 { 539 return ""; 540 } 541 542 545 public int getClassWidth() 546 { 547 return this.classWidth; 548 } 549 550 553 public void setClassWidth(int classWidth) 554 { 555 this.classWidth = classWidth; 556 } 557 558 561 public int getHostWidth() 562 { 563 return this.hostWidth; 564 } 565 566 569 public void setHostWidth(int hostWidth) 570 { 571 this.hostWidth = hostWidth; 572 } 573 574 577 public int getChannelWidth() 578 { 579 return this.channelWidth; 580 } 581 582 585 public void setChannelWidth(int channelWidth) 586 { 587 this.channelWidth = channelWidth; 588 } 589 590 593 public int getThreadWidth() 594 { 595 return this.threadWidth; 596 } 597 598 601 public void setThreadWidth(int threadWidth) 602 { 603 this.threadWidth = threadWidth; 604 } 605 606 609 protected void justify(StringBuffer out, StringBuffer in, int width) 610 { 611 justify(out, in.toString(), width); 612 } 613 614 617 protected void justify(StringBuffer out, String in, int width) 618 { 619 out.append(in); 620 for (int i=in.length(); i<width; i++) 621 out.append(" "); 622 } 623 } 624 | Popular Tags |