001: package org.jzonic.jlo;
002:
003: import org.apache.commons.logging.Log;
004: import org.jzonic.jlo.filter.LogFilter;
005: import org.jzonic.jlo.formatter.CallerStack;
006: import org.jzonic.jlo.processor.LogProcessor;
007: import org.jzonic.jlo.processor.LogProcessorFactory;
008:
009: import java.util.Vector;
010:
011: /**
012: */
013: public class Logger implements Log {
014:
015: private static final LogProcessor processor = LogProcessorFactory
016: .getLogProcessor();
017: private Vector generators;
018: private int targets;
019: private LogFilter filter = null;
020: private String sourceClass = null;
021: private String sourceMethod = null;
022: private String name;
023: private String configName;
024:
025: /**
026: * Constructor for the Logger object
027: */
028: private Logger() {
029: }
030:
031: public Logger(String name, int targets, String configName) {
032: this .name = name;
033: this .targets = targets;
034: generators = new Vector();
035: this .configName = configName;
036: }
037:
038: public void addLogGenerator(LogGenerator lg) {
039: generators.add(lg);
040: }
041:
042: public Vector getLogGenerators() {
043: return generators;
044: }
045:
046: /**
047: * Log a message, with no arguments.
048: */
049: public void log(Target curtarget, String msg, Throwable thrown) {
050: if ((curtarget.intValue() & targets) == curtarget.intValue()) {
051: LogRecord lr = new LogRecord(msg, curtarget);
052: lr.setLoggerName(name);
053: String cn = CallerStack.getCallerClass(this .getClass())
054: .getName();
055: lr.setSourceClassName(cn);
056: lr.setTarget(curtarget);
057: lr.setThrown(thrown);
058: lr.setConfigurationName(configName);
059: lr.setNDC(NDC.getAsString());
060: long elp = -1;
061: if (TimeTracker.isTracking()) {
062: elp = TimeTracker.getEllapsedTime();
063: }
064: lr.setElapsed(elp);
065: log(lr);
066: }
067: }
068:
069: /**
070: * Log a message, with associated Throwable information.
071: */
072: public void log(Target target, String msg) {
073: log(target, msg, null);
074: }
075:
076: /**
077: * Log a LogRecord.
078: *
079: */
080: public void log(LogRecord record) {
081: for (int i = 0; i < generators.size(); i++) {
082: LogGenerator lg = (LogGenerator) generators.get(i);
083: if (filter != null) {
084: if (filter.match(record.getMessage())) {
085: processor.processEvent(lg, record);
086: }
087: } else {
088: processor.processEvent(lg, record);
089: }
090: }
091: }
092:
093: /**
094: */
095: public void setTargets(int targets) {
096: this .targets = targets;
097: }
098:
099: public boolean isLoggable(Target target) {
100: if ((target.intValue() & targets) == target.intValue()) {
101: return true;
102: } else {
103: return false;
104: }
105: }
106:
107: /**
108: */
109: public int getTargets() {
110: return targets;
111: }
112:
113: /**
114: * Sets the loggerName attribute of the Logger object
115: */
116: public void setLoggerName(String name) {
117: this .name = name;
118: }
119:
120: /**
121: * Gets the loggerName attribute of the Logger object
122: */
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: // ------------------------------------
200: 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: /** Getter for property filter.
225: * @return Value of property filter.
226: *
227: */
228: public LogFilter getFilter() {
229: return filter;
230: }
231:
232: /** Setter for property filter.
233: * @param filter New value of property filter.
234: *
235: */
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: }
|