KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > Logger


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 JavaDoc;
11 /**
12  */

13 public class Logger implements Log {
14         
15     private static final LogProcessor processor = LogProcessorFactory.getLogProcessor();
16     private Vector JavaDoc generators;
17     private int targets;
18     private LogFilter filter = null;
19     private String JavaDoc sourceClass = null;
20     private String JavaDoc sourceMethod = null;
21     private String JavaDoc name;
22     private String JavaDoc configName;
23     /**
24      * Constructor for the Logger object
25      */

26     private Logger() {
27     }
28     
29     public Logger(String JavaDoc name,int targets,String JavaDoc configName) {
30         this.name = name;
31         this.targets = targets;
32         generators = new Vector JavaDoc();
33         this.configName = configName;
34     }
35     
36     public void addLogGenerator(LogGenerator lg) {
37         generators.add(lg);
38     }
39     
40     public Vector JavaDoc getLogGenerators() {
41         return generators;
42     }
43     /**
44      * Log a message, with no arguments.
45      */

46     public void log(Target curtarget, String JavaDoc msg,Throwable JavaDoc thrown) {
47         if ( (curtarget.intValue() & targets ) == curtarget.intValue() ) {
48             LogRecord lr = new LogRecord(msg, curtarget);
49             lr.setLoggerName(name);
50             String JavaDoc 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     /**
67      * Log a message, with associated Throwable information.
68      */

69     public void log(Target target, String JavaDoc msg) {
70         log ( target,msg,null);
71     }
72     
73     
74     /**
75      * Log a LogRecord.
76      *
77      */

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     /**
93      */

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     /**
107      */

108     public int getTargets() {
109         return targets;
110     }
111     
112     /**
113      * Sets the loggerName attribute of the Logger object
114      */

115     public void setLoggerName(String JavaDoc name) {
116         this.name = name;
117     }
118     
119     
120     /**
121      * Gets the loggerName attribute of the Logger object
122      */

123     public String JavaDoc getLoggerName() {
124         return name;
125     }
126     
127     public void trace(Object JavaDoc obj) {
128         if ( obj != null ) {
129             log(Target.trace, obj.toString(),null);
130         }
131     }
132     
133     public void trace(Object JavaDoc obj, Throwable JavaDoc throwable) {
134         if ( obj != null ) {
135             log(Target.trace, obj.toString(),throwable);
136         }
137     }
138         
139     public void info(Object JavaDoc obj) {
140         if ( obj != null ) {
141             log(Target.info, obj.toString(),null);
142         }
143     }
144     
145     public void info(Object JavaDoc obj, Throwable JavaDoc throwable) {
146         if ( obj != null ) {
147             log(Target.info, obj.toString(),throwable);
148         }
149     }
150     
151     public void debug(Object JavaDoc obj) {
152         if ( obj != null ) {
153             log(Target.debug, obj.toString(),null);
154         }
155     }
156     
157     public void debug(Object JavaDoc obj, Throwable JavaDoc throwable) {
158         if ( obj != null ) {
159             log(Target.debug, obj.toString(),throwable);
160         }
161     }
162     
163     public void warn(Object JavaDoc obj) {
164         if ( obj != null ) {
165             log(Target.warn, obj.toString(),null);
166         }
167     }
168     
169     public void warn(Object JavaDoc obj, Throwable JavaDoc throwable) {
170         if ( obj != null ) {
171             log(Target.warn, obj.toString(),throwable);
172         }
173     }
174         
175     public void error(Object JavaDoc obj) {
176         if ( obj != null ) {
177             log(Target.error, obj.toString(),null);
178         }
179     }
180     
181     public void error(Object JavaDoc obj, Throwable JavaDoc throwable) {
182         if ( obj != null ) {
183             log(Target.error, obj.toString(),throwable);
184         }
185     }
186     
187     public void fatal(Object JavaDoc obj) {
188         if ( obj != null ) {
189             log(Target.fatal, obj.toString(),null);
190         }
191     }
192     
193     public void fatal(Object JavaDoc obj, Throwable JavaDoc 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 JavaDoc toString() {
241         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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