1 2 package org.jzonic.jlo; 3 4 import org.apache.commons.logging.Log; 5 import org.jzonic.jlo.filter.LogFilter; 6 import org.jzonic.jlo.formatter.CallerStack; 7 import org.jzonic.jlo.processor.LogProcessor; 8 import org.jzonic.jlo.processor.LogProcessorFactory; 9 10 import java.util.Vector ; 11 13 public class Logger implements Log { 14 15 private static final LogProcessor processor = LogProcessorFactory.getLogProcessor(); 16 private Vector generators; 17 private int targets; 18 private LogFilter filter = null; 19 private String sourceClass = null; 20 private String sourceMethod = null; 21 private String name; 22 private String configName; 23 26 private Logger() { 27 } 28 29 public Logger(String name,int targets,String configName) { 30 this.name = name; 31 this.targets = targets; 32 generators = new Vector (); 33 this.configName = configName; 34 } 35 36 public void addLogGenerator(LogGenerator lg) { 37 generators.add(lg); 38 } 39 40 public Vector getLogGenerators() { 41 return generators; 42 } 43 46 public void log(Target curtarget, String msg,Throwable thrown) { 47 if ( (curtarget.intValue() & targets ) == curtarget.intValue() ) { 48 LogRecord lr = new LogRecord(msg, curtarget); 49 lr.setLoggerName(name); 50 String cn = CallerStack.getCallerClass(this.getClass()).getName(); 51 lr.setSourceClassName(cn); 52 lr.setTarget(curtarget); 53 lr.setThrown(thrown); 54 lr.setConfigurationName(configName); 55 lr.setNDC(NDC.getAsString()); 56 long elp = -1; 57 if ( TimeTracker.isTracking() ) { 58 elp = TimeTracker.getEllapsedTime(); 59 } 60 lr.setElapsed(elp); 61 log(lr); 62 } 63 } 64 65 66 69 public void log(Target target, String msg) { 70 log ( target,msg,null); 71 } 72 73 74 78 public void log(LogRecord record) { 79 for ( int i = 0; i < generators.size();i++) { 80 LogGenerator lg = (LogGenerator)generators.get(i); 81 if ( filter != null ) { 82 if ( filter.match(record.getMessage()) ) { 83 processor.processEvent(lg, record); 84 } 85 } 86 else { 87 processor.processEvent(lg, record); 88 } 89 } 90 } 91 92 94 public void setTargets(int targets) { 95 this.targets = targets; 96 } 97 98 public boolean isLoggable(Target target) { 99 if ( (target.intValue() & targets ) == target.intValue() ) { 100 return true; 101 } 102 else { 103 return false; 104 } 105 } 106 108 public int getTargets() { 109 return targets; 110 } 111 112 115 public void setLoggerName(String name) { 116 this.name = name; 117 } 118 119 120 123 public String getLoggerName() { 124 return name; 125 } 126 127 public void trace(Object obj) { 128 if ( obj != null ) { 129 log(Target.trace, obj.toString(),null); 130 } 131 } 132 133 public void trace(Object obj, Throwable throwable) { 134 if ( obj != null ) { 135 log(Target.trace, obj.toString(),throwable); 136 } 137 } 138 139 public void info(Object obj) { 140 if ( obj != null ) { 141 log(Target.info, obj.toString(),null); 142 } 143 } 144 145 public void info(Object obj, Throwable throwable) { 146 if ( obj != null ) { 147 log(Target.info, obj.toString(),throwable); 148 } 149 } 150 151 public void debug(Object obj) { 152 if ( obj != null ) { 153 log(Target.debug, obj.toString(),null); 154 } 155 } 156 157 public void debug(Object obj, Throwable throwable) { 158 if ( obj != null ) { 159 log(Target.debug, obj.toString(),throwable); 160 } 161 } 162 163 public void warn(Object obj) { 164 if ( obj != null ) { 165 log(Target.warn, obj.toString(),null); 166 } 167 } 168 169 public void warn(Object obj, Throwable throwable) { 170 if ( obj != null ) { 171 log(Target.warn, obj.toString(),throwable); 172 } 173 } 174 175 public void error(Object obj) { 176 if ( obj != null ) { 177 log(Target.error, obj.toString(),null); 178 } 179 } 180 181 public void error(Object obj, Throwable throwable) { 182 if ( obj != null ) { 183 log(Target.error, obj.toString(),throwable); 184 } 185 } 186 187 public void fatal(Object obj) { 188 if ( obj != null ) { 189 log(Target.fatal, obj.toString(),null); 190 } 191 } 192 193 public void fatal(Object obj, Throwable throwable) { 194 if ( obj != null ) { 195 log(Target.fatal, obj.toString(),throwable); 196 } 197 } 198 199 public boolean isTraceEnabled() { 201 return isLoggable(Target.trace); 202 } 203 204 public boolean isInfoEnabled() { 205 return isLoggable(Target.info); 206 } 207 208 public boolean isDebugEnabled() { 209 return isLoggable(Target.debug); 210 } 211 212 public boolean isWarnEnabled() { 213 return isLoggable(Target.warn); 214 } 215 216 public boolean isErrorEnabled() { 217 return isLoggable(Target.error); 218 } 219 220 public boolean isFatalEnabled() { 221 return isLoggable(Target.fatal); 222 } 223 224 228 public LogFilter getFilter() { 229 return filter; 230 } 231 232 236 public void setFilter(LogFilter filter) { 237 this.filter = filter; 238 } 239 240 public String toString() { 241 StringBuffer buffer = new StringBuffer (); 242 buffer.append("logger[ name="); 243 buffer.append(getLoggerName()); 244 buffer.append(", targets="); 245 buffer.append(getTargets()); 246 buffer.append(",filter="); 247 if ( filter != null ) { 248 buffer.append(getFilter().getClass().getName()); 249 } 250 buffer.append("]"); 251 return buffer.toString(); 252 } 253 254 } 255 | Popular Tags |