KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > LogContext


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * LogContext.java
29  * ---------------
30  * (C)opyright 2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: LogContext.java,v 1.3 2005/10/18 13:24:19 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 26-Apr-2004 : Initial version (TM);
40  *
41  */

42
43 package org.jfree.util;
44
45 /**
46  * A log context.
47  *
48  * @author Thomas Morgner
49  */

50 public class LogContext {
51
52     /** The prefix string. */
53     private String JavaDoc contextPrefix;
54
55     /**
56      * Creates a new log context.
57      *
58      * @param contextPrefix the prefix.
59      */

60     public LogContext(final String JavaDoc contextPrefix) {
61         this.contextPrefix = contextPrefix;
62     }
63
64     /**
65      * Returns true, if the log level allows debug messages to be
66      * printed.
67      *
68      * @return true, if messages with an log level of DEBUG are allowed.
69      */

70     public boolean isDebugEnabled() {
71         return Log.isDebugEnabled();
72     }
73
74     /**
75      * Returns true, if the log level allows informational
76      * messages to be printed.
77      *
78      * @return true, if messages with an log level of INFO are allowed.
79      */

80     public boolean isInfoEnabled() {
81         return Log.isInfoEnabled();
82     }
83
84     /**
85      * Returns true, if the log level allows warning messages to be
86      * printed.
87      *
88      * @return true, if messages with an log level of WARN are allowed.
89      */

90     public boolean isWarningEnabled() {
91         return Log.isWarningEnabled();
92     }
93
94     /**
95      * Returns true, if the log level allows error messages to be
96      * printed.
97      *
98      * @return true, if messages with an log level of ERROR are allowed.
99      */

100     public boolean isErrorEnabled() {
101         return Log.isErrorEnabled();
102     }
103
104
105     /**
106      * A convenience method for logging a 'debug' message.
107      *
108      * @param message the message.
109      */

110     public void debug(final Object JavaDoc message) {
111         log(LogTarget.DEBUG, message);
112     }
113
114     /**
115      * A convenience method for logging a 'debug' message.
116      *
117      * @param message the message.
118      * @param e the exception.
119      */

120     public void debug(final Object JavaDoc message, final Exception JavaDoc e) {
121         log(LogTarget.DEBUG, message, e);
122     }
123
124     /**
125      * A convenience method for logging an 'info' message.
126      *
127      * @param message the message.
128      */

129     public void info(final Object JavaDoc message) {
130         log(LogTarget.INFO, message);
131     }
132
133     /**
134      * A convenience method for logging an 'info' message.
135      *
136      * @param message the message.
137      * @param e the exception.
138      */

139     public void info(final Object JavaDoc message, final Exception JavaDoc e) {
140         log(LogTarget.INFO, message, e);
141     }
142
143     /**
144      * A convenience method for logging a 'warning' message.
145      *
146      * @param message the message.
147      */

148     public void warn(final Object JavaDoc message) {
149         log(LogTarget.WARN, message);
150     }
151
152     /**
153      * A convenience method for logging a 'warning' message.
154      *
155      * @param message the message.
156      * @param e the exception.
157      */

158     public void warn(final Object JavaDoc message, final Exception JavaDoc e) {
159         log(LogTarget.WARN, message, e);
160     }
161
162     /**
163      * A convenience method for logging an 'error' message.
164      *
165      * @param message the message.
166      */

167     public void error(final Object JavaDoc message) {
168         log(LogTarget.ERROR, message);
169     }
170
171     /**
172      * A convenience method for logging an 'error' message.
173      *
174      * @param message the message.
175      * @param e the exception.
176      */

177     public void error(final Object JavaDoc message, final Exception JavaDoc e) {
178         log(LogTarget.ERROR, message, e);
179     }
180
181     /**
182      * Logs a message to the main log stream. All attached log targets will also
183      * receive this message. If the given log-level is higher than the given debug-level
184      * in the main config file, no logging will be done.
185      *
186      * @param level log level of the message.
187      * @param message text to be logged.
188      */

189     public void log(final int level, final Object JavaDoc message) {
190         if (this.contextPrefix != null) {
191             Log.getInstance().doLog(level, new Log.SimpleMessage(this.contextPrefix, ":", message));
192         }
193         else {
194             Log.getInstance().doLog(level, message);
195         }
196     }
197
198     /**
199      * Logs a message to the main log stream. All attached logTargets will also
200      * receive this message. If the given log-level is higher than the given debug-level
201      * in the main config file, no logging will be done.
202      * <p/>
203      * The exception's stacktrace will be appended to the log-stream
204      *
205      * @param level log level of the message.
206      * @param message text to be logged.
207      * @param e the exception, which should be logged.
208      */

209     public void log(final int level, final Object JavaDoc message, final Exception JavaDoc e) {
210         if (this.contextPrefix != null) {
211             Log.getInstance().doLog(
212                 level, new Log.SimpleMessage(this.contextPrefix, ":", message), e
213             );
214         }
215         else {
216             Log.getInstance().doLog(level, message, e);
217         }
218     }
219
220     /**
221      * Tests this object for equality with an arbitrary object.
222      *
223      * @param o the object to test against (<code>null</code> permitted).
224      *
225      * @return A boolean.
226      */

227     public boolean equals(final Object JavaDoc o) {
228         if (this == o) {
229             return true;
230         }
231         if (!(o instanceof LogContext)) {
232             return false;
233         }
234
235         final LogContext logContext = (LogContext) o;
236
237         if (this.contextPrefix != null)
238         {
239             if (!this.contextPrefix.equals(logContext.contextPrefix)) {
240                 return false;
241             }
242         }
243         else {
244             if (logContext.contextPrefix != null) {
245                 return false;
246             }
247         }
248
249         return true;
250     }
251
252     /**
253      * Returns a hashcode.
254      *
255      * @return The hashcode.
256      */

257     public int hashCode() {
258         return (this.contextPrefix != null ? this.contextPrefix.hashCode() : 0);
259     }
260 }
261
Popular Tags