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 2002-2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner (taquera@sherito.org);
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: Log.java,v 1.5 2006/06/08 17:42:20 taqua Exp $
39
* 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
40
* 11-Jun-2003 : Removing LogTarget did not work.
44
package org.jfree.util;
46
import java.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.HashMap;
51
* A simple logging facility. Create a class implementing the {@link org.jfree.util.LogTarget}
52
* interface to use this feature.
54
* @author Thomas Morgner
59
* A simple message class.
61
public static class SimpleMessage {
66
private String message;
71
private Object[] param;
74
* Creates a new message.
76
* @param message the message text.
77
* @param param1 parameter 1.
79
public SimpleMessage(final String message, final Object param1) {
80
this.message = message;
81
this.param = new Object[]{param1};
85
* Creates a new message.
87
* @param message the message text.
88
* @param param1 parameter 1.
89
* @param param2 parameter 2.
91
public SimpleMessage(final String message, final Object param1,
92
final Object param2) {
93
this.message = message;
94
this.param = new Object[]{param1, param2};
98
* Creates a new message.
100
* @param message the message text.
101
* @param param1 parameter 1.
102
* @param param2 parameter 2.
103
* @param param3 parameter 3.
105
public SimpleMessage(final String message, final Object param1,
106
final Object param2, final Object param3) {
107
this.message = message;
108
this.param = new Object[]{param1, param2, param3};
112
* Creates a new message.
114
* @param message the message text.
115
* @param param1 parameter 1.
116
* @param param2 parameter 2.
117
* @param param3 parameter 3.
118
* @param param4 parameter 4.
120
public SimpleMessage(final String message, final Object param1,
121
final Object param2, final Object param3,
122
final Object param4) {
123
this.message = message;
124
this.param = new Object[]{param1, param2, param3, param4};
128
* Creates a new message.
130
* @param message the message text.
131
* @param param the parameters.
133
public SimpleMessage(final String message, final Object[] param) {
134
this.message = message;
139
* Returns a string representation of the message (useful for debugging).
141
* @return the string.
143
public String toString() {
144
final StringBuffer b = new StringBuffer();
145
b.append(this.message);
146
if (this.param != null) {
147
for (int i = 0; i < this.param.length; i++) {
148
b.append(this.param[i]);
157
* The logging threshold.
159
private int debuglevel;
162
* Storage for the log targets.
164
private LogTarget[] logTargets;
166
/** The log contexts. */
167
private HashMap logContexts;
170
* the singleton instance of the Log system.
172
private static Log singleton;
175
* Creates a new Log instance. The Log is used to manage the log targets.
178
this.logContexts = new HashMap();
179
this.logTargets = new LogTarget[0];
180
this.debuglevel = 100;
184
* Returns the singleton Log instance. A new instance is created if necessary.
186
* @return the singleton instance.
188
public static synchronized Log getInstance() {
189
if (singleton == null) {
190
singleton = new Log();
196
* Redefines or clears the currently used log instance.
198
* @param log the new log instance or null, to return to the default implementation.
200
protected static synchronized void defineLog(final Log log) {
205
* Returns the currently defined debug level. The higher the level, the more details
208
* @return the debug level.
210
public int getDebuglevel() {
211
return this.debuglevel;
215
* Defines the debug level for the log system.
217
* @param debuglevel the new debug level
218
* @see #getDebuglevel()
220
protected void setDebuglevel(final int debuglevel) {
221
this.debuglevel = debuglevel;
225
* Adds a log target to this facility. Log targets get informed, via the LogTarget interface,
226
* whenever a message is logged with this class.
228
* @param target the target.
230
public synchronized void addTarget(final LogTarget target) {
231
if (target == null) {
232
throw new NullPointerException();
234
final LogTarget[] data = new LogTarget[this.logTargets.length + 1];
235
System.arraycopy(this.logTargets, 0, data, 0, this.logTargets.length);
236
data[this.logTargets.length] = target;
237
this.logTargets = data;
241
* Removes a log target from this facility.
243
* @param target the target to remove.
245
public synchronized void removeTarget(final LogTarget target) {
246
if (target == null) {
247
throw new NullPointerException();
249
final ArrayList l = new ArrayList();
250
l.addAll(Arrays.asList(this.logTargets));
253
final LogTarget[] targets = new LogTarget[l.size()];
254
this.logTargets = (LogTarget[]) l.toArray(targets);
258
* Returns the registered logtargets.
260
* @return the logtargets.
262
public LogTarget[] getTargets() {
263
return (LogTarget[]) this.logTargets.clone();
267
* Replaces all log targets by the given target.
269
* @param target the new and only logtarget.
271
public synchronized void replaceTargets(final LogTarget target) {
272
if (target == null) {
273
throw new NullPointerException();
275
this.logTargets = new LogTarget[]{target};
279
* A convenience method for logging a 'debug' message.
281
* @param message the message.
283
public static void debug(final Object message) {
284
log(LogTarget.DEBUG, message);
288
* A convenience method for logging a 'debug' message.
290
* @param message the message.
291
* @param e the exception.
293
public static void debug(final Object message, final Exception e) {
294
log(LogTarget.DEBUG, message, e);
298
* A convenience method for logging an 'info' message.
300
* @param message the message.
302
public static void info(final Object message) {
303
log(LogTarget.INFO, message);
307
* A convenience method for logging an 'info' message.
309
* @param message the message.
310
* @param e the exception.
312
public static void info(final Object message, final Exception e) {
313
log(LogTarget.INFO, message, e);
317
* A convenience method for logging a 'warning' message.
319
* @param message the message.
321
public static void warn(final Object message) {
322
log(LogTarget.WARN, message);
326
* A convenience method for logging a 'warning' message.
328
* @param message the message.
329
* @param e the exception.
331
public static void warn(final Object message, final Exception e) {
332
log(LogTarget.WARN, message, e);
336
* A convenience method for logging an 'error' message.
338
* @param message the message.
340
public static void error(final Object message) {
341
log(LogTarget.ERROR, message);
345
* A convenience method for logging an 'error' message.
347
* @param message the message.
348
* @param e the exception.
350
public static void error(final Object message, final Exception e) {
351
log(LogTarget.ERROR, message, e);
355
* Logs a message to the main log stream. All attached log targets will also
356
* receive this message. If the given log-level is higher than the given debug-level
357
* in the main config file, no logging will be done.
359
* @param level log level of the message.
360
* @param message text to be logged.
362
protected void doLog(int level, final Object message) {
366
if (level <= this.debuglevel) {
367
for (int i = 0; i < this.logTargets.length; i++) {
368
final LogTarget t = this.logTargets[i];
369
t.log(level, message);
375
* Logs a message to the main log stream. All attached log targets will also
376
* receive this message. If the given log-level is higher than the given debug-level
377
* in the main config file, no logging will be done.
379
* @param level log level of the message.
380
* @param message text to be logged.
382
public static void log(final int level, final Object message) {
383
getInstance().doLog(level, message);
387
* Logs a message to the main log stream. All attached logTargets will also
388
* receive this message. If the given log-level is higher than the given debug-level
389
* in the main config file, no logging will be done.
391
* The exception's stacktrace will be appended to the log-stream
393
* @param level log level of the message.
394
* @param message text to be logged.
395
* @param e the exception, which should be logged.
397
public static void log(final int level, final Object message, final Exception e) {
398
getInstance().doLog(level, message, e);
402
* Logs a message to the main log stream. All attached logTargets will also
403
* receive this message. If the given log-level is higher than the given debug-level
404
* in the main config file, no logging will be done.
406
* The exception's stacktrace will be appended to the log-stream
408
* @param level log level of the message.
409
* @param message text to be logged.
410
* @param e the exception, which should be logged.
412
protected void doLog(int level, final Object message, final Exception e) {
417
if (level <= this.debuglevel) {
418
for (int i = 0; i < this.logTargets.length; i++) {
419
final LogTarget t = this.logTargets[i];
420
t.log(level, message, e);
426
* Initializes the logging system. Implementors should
427
* override this method to supply their own log configuration.
430
// this method is intentionally empty.
434
* Returns true, if the log level allows debug messages to be
437
* @return true, if messages with an log level of DEBUG are allowed.
439
public static boolean isDebugEnabled() {
440
return getInstance().getDebuglevel() >= LogTarget.DEBUG;
444
* Returns true, if the log level allows informational
445
* messages to be printed.
447
* @return true, if messages with an log level of INFO are allowed.
449
public static boolean isInfoEnabled() {
450
return getInstance().getDebuglevel() >= LogTarget.INFO;
454
* Returns true, if the log level allows warning messages to be
457
* @return true, if messages with an log level of WARN are allowed.
459
public static boolean isWarningEnabled() {
460
return getInstance().getDebuglevel() >= LogTarget.WARN;
464
* Returns true, if the log level allows error messages to be
467
* @return true, if messages with an log level of ERROR are allowed.
469
public static boolean isErrorEnabled() {
470
return getInstance().getDebuglevel() >= LogTarget.ERROR;
474
* Creates a log context.
476
* @param context the class (<code>null</code> not permitted).
478
* @return A log context.
480
public static LogContext createContext(final Class context) {
481
return createContext(context.getName());
485
* Creates a log context.
487
* @param context the label for the context.
489
* @return A log context.
491
public static LogContext createContext(final String context) {
492
return getInstance().internalCreateContext(context);
496
* Creates a log context.
498
* @param context the name of the logging context (a common prefix).
500
* @return A log context.
502
protected LogContext internalCreateContext(final String context) {
503
synchronized (this) {
504
LogContext ctx = (LogContext) this.logContexts.get(context);
506
ctx = new LogContext(context);
507
this.logContexts.put(context, ctx);