1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
30
* (C)opyright 2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: LogContext.java,v 1.3 2005/10/18 13:24:19 mungady Exp $
39
* 26-Apr-2004 : Initial version (TM);
43
package org.jfree.util;
48
* @author Thomas Morgner
50
public class LogContext {
52
/** The prefix string. */
53
private String contextPrefix;
56
* Creates a new log context.
58
* @param contextPrefix the prefix.
60
public LogContext(final String contextPrefix) {
61
this.contextPrefix = contextPrefix;
65
* Returns true, if the log level allows debug messages to be
68
* @return true, if messages with an log level of DEBUG are allowed.
70
public boolean isDebugEnabled() {
71
return Log.isDebugEnabled();
75
* Returns true, if the log level allows informational
76
* messages to be printed.
78
* @return true, if messages with an log level of INFO are allowed.
80
public boolean isInfoEnabled() {
81
return Log.isInfoEnabled();
85
* Returns true, if the log level allows warning messages to be
88
* @return true, if messages with an log level of WARN are allowed.
90
public boolean isWarningEnabled() {
91
return Log.isWarningEnabled();
95
* Returns true, if the log level allows error messages to be
98
* @return true, if messages with an log level of ERROR are allowed.
100
public boolean isErrorEnabled() {
101
return Log.isErrorEnabled();
106
* A convenience method for logging a 'debug' message.
108
* @param message the message.
110
public void debug(final Object message) {
111
log(LogTarget.DEBUG, message);
115
* A convenience method for logging a 'debug' message.
117
* @param message the message.
118
* @param e the exception.
120
public void debug(final Object message, final Exception e) {
121
log(LogTarget.DEBUG, message, e);
125
* A convenience method for logging an 'info' message.
127
* @param message the message.
129
public void info(final Object message) {
130
log(LogTarget.INFO, message);
134
* A convenience method for logging an 'info' message.
136
* @param message the message.
137
* @param e the exception.
139
public void info(final Object message, final Exception e) {
140
log(LogTarget.INFO, message, e);
144
* A convenience method for logging a 'warning' message.
146
* @param message the message.
148
public void warn(final Object message) {
149
log(LogTarget.WARN, message);
153
* A convenience method for logging a 'warning' message.
155
* @param message the message.
156
* @param e the exception.
158
public void warn(final Object message, final Exception e) {
159
log(LogTarget.WARN, message, e);
163
* A convenience method for logging an 'error' message.
165
* @param message the message.
167
public void error(final Object message) {
168
log(LogTarget.ERROR, message);
172
* A convenience method for logging an 'error' message.
174
* @param message the message.
175
* @param e the exception.
177
public void error(final Object message, final Exception e) {
178
log(LogTarget.ERROR, message, e);
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.
186
* @param level log level of the message.
187
* @param message text to be logged.
189
public void log(final int level, final Object message) {
190
if (this.contextPrefix != null) {
191
Log.getInstance().doLog(level, new Log.SimpleMessage(this.contextPrefix, ":", message));
194
Log.getInstance().doLog(level, message);
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.
203
* The exception's stacktrace will be appended to the log-stream
205
* @param level log level of the message.
206
* @param message text to be logged.
207
* @param e the exception, which should be logged.
209
public void log(final int level, final Object message, final Exception e) {
210
if (this.contextPrefix != null) {
211
Log.getInstance().doLog(
212
level, new Log.SimpleMessage(this.contextPrefix, ":", message), e
216
Log.getInstance().doLog(level, message, e);
221
* Tests this object for equality with an arbitrary object.
223
* @param o the object to test against (<code>null</code> permitted).
227
public boolean equals(final Object o) {
231
if (!(o instanceof LogContext)) {
235
final LogContext logContext = (LogContext) o;
237
if (this.contextPrefix != null)
239
if (!this.contextPrefix.equals(logContext.contextPrefix)) {
244
if (logContext.contextPrefix != null) {
253
* Returns a hashcode.
255
* @return The hashcode.
257
public int hashCode() {
258
return (this.contextPrefix != null ? this.contextPrefix.hashCode() : 0);