KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PrintStreamLogTarget.java
29  * -------------------------
30  * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: PrintStreamLogTarget.java,v 1.6 2005/10/18 13:24:19 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 02-Dec-2003 : Initial version
40  * 11-Feb-2004 : Added missing Javadocs (DG);
41  *
42  */

43
44 package org.jfree.util;
45
46 import java.io.PrintStream JavaDoc;
47 import java.io.Serializable JavaDoc;
48
49 /**
50  * A log target that sends output to a {@link PrintStream}.
51  *
52  * @author Thomas Morgner
53  */

54 public class PrintStreamLogTarget implements LogTarget, Serializable JavaDoc {
55
56   /** For serialization. */
57   private static final long serialVersionUID = 6510564403264504688L;
58
59   /** The printstream we use .. */
60   private PrintStream JavaDoc printStream;
61
62   /**
63    * The default constructor. Initializes this target with the system.out
64    * stream.
65    * <p>
66    * All {@link org.jfree.util.LogTarget} implementations need a default
67    * constructor.
68    */

69   public PrintStreamLogTarget() {
70     this (System.out);
71   }
72
73   /**
74    * The default constructor. Initializes this target with the given stream.
75    * <p>
76    * @param printStream the print stream that is used to write the content.
77    */

78   public PrintStreamLogTarget(final PrintStream JavaDoc printStream) {
79     if (printStream == null) {
80       throw new NullPointerException JavaDoc();
81     }
82     this.printStream = printStream;
83   }
84
85   /**
86    * Logs a message to the main log stream. All attached logStreams will also
87    * receive this message. If the given log-level is higher than the given
88    * debug-level in the main config file, no logging will be done.
89    *
90    * @param level log level of the message.
91    * @param message text to be logged.
92    */

93   public void log(int level, final Object JavaDoc message) {
94     if (level > 3) {
95       level = 3;
96     }
97     this.printStream.print(LEVELS[level]);
98     this.printStream.println(message);
99     if (level < 3) {
100       System.out.flush();
101     }
102   }
103
104   /**
105    * logs an message to the main-log stream. All attached logStreams will also
106    * receive this message. If the given log-level is higher than the given
107    * debug-level in the main config file, no logging will be done.
108    *
109    * The exception's stacktrace will be appended to the log-stream
110    *
111    * @param level log level of the message.
112    * @param message text to be logged.
113    * @param e the exception, which should be logged.
114    */

115   public void log(int level, final Object JavaDoc message, final Exception JavaDoc e) {
116     if (level > 3) {
117       level = 3;
118     }
119     this.printStream.print(LEVELS[level]);
120     this.printStream.println(message);
121     e.printStackTrace(this.printStream);
122     if (level < 3) {
123       System.out.flush();
124     }
125   }
126 }
127
Popular Tags