1 16 package org.apache.log4j.lf5.util; 17 18 import org.apache.log4j.lf5.LogLevel; 19 import org.apache.log4j.lf5.LogRecord; 20 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 24 29 30 32 public class AdapterLogRecord extends LogRecord { 33 37 41 private static LogLevel severeLevel = null; 45 46 private static StringWriter sw = new StringWriter (); 47 private static PrintWriter pw = new PrintWriter (sw); 48 49 public AdapterLogRecord() { 53 super(); 54 } 55 56 public void setCategory(String category) { 60 super.setCategory(category); 61 super.setLocation(getLocationInfo(category)); 62 } 63 64 public boolean isSevereLevel() { 65 if (severeLevel == null) return false; 66 return severeLevel.equals(getLevel()); 67 } 68 69 public static void setSevereLevel(LogLevel level) { 70 severeLevel = level; 71 } 72 73 public static LogLevel getSevereLevel() { 74 return severeLevel; 75 } 76 77 protected String getLocationInfo(String category) { 81 String stackTrace = stackTraceToString(new Throwable ()); 82 String line = parseLine(stackTrace, category); 83 return line; 84 } 85 86 protected String stackTraceToString(Throwable t) { 87 String s = null; 88 89 synchronized (sw) { 90 t.printStackTrace(pw); 91 s = sw.toString(); 92 sw.getBuffer().setLength(0); 93 } 94 95 return s; 96 } 97 98 protected String parseLine(String trace, String category) { 99 int index = trace.indexOf(category); 100 if (index == -1) return null; 101 trace = trace.substring(index); 102 trace = trace.substring(0, trace.indexOf(")") + 1); 103 return trace; 104 } 105 109 } 113 114 | Popular Tags |