KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > logging > HTMLLog


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /*
66  * HTMLLog.java
67  *
68  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.logging;
71
72 import com.jcorporate.expresso.core.misc.DateTime;
73
74 import java.io.BufferedOutputStream JavaDoc;
75 import java.io.File JavaDoc;
76 import java.io.FileOutputStream JavaDoc;
77 import java.io.PrintStream JavaDoc;
78
79
80 /**
81  * HTMLLog
82  *
83  * @author Michael Nash
84  * @deprecated since 5.6, use log4j
85  */

86 class HTMLLog {
87     private String JavaDoc logFileName = ("");
88     private String JavaDoc logTitle = ("");
89     private int logMax = 0;
90     private int maxBytes = 5000000;
91
92     /* Do we also print our output on the screen? */
93     private boolean echoOutput = false;
94     private static String JavaDoc seperator = ("|");
95     private static final String JavaDoc thisClass = HTMLLog.class.getName() + ".";
96
97     /**
98      * Constructor
99      *
100      * @param fileName
101      */

102     public HTMLLog(String JavaDoc fileName)
103             throws LogException {
104         setFileName(fileName);
105         logTitle = ("Application Log");
106         logMax = 500;
107     } /* HTMLLog(String) */
108
109     /**
110      * Constructor
111      *
112      * @param newlogTitle
113      * @param newlogMax
114      */

115     public HTMLLog(String JavaDoc newlogTitle, int newlogMax) {
116         logTitle = newlogTitle;
117         logMax = newlogMax;
118     } /* HTMLLog(String, int) */
119
120     /**
121      * Return the character being used as a seperator between the fields
122      *
123      * @return String Character used as seperator
124      */

125     public static String JavaDoc getSeperator() {
126         return seperator;
127     } /* getSeperator() */
128
129     /**
130      * Log the message from the given object
131      *
132      * @param objectName Calling object
133      * @param logMessage Message to write
134      * @param theColor Color to write the message in
135      */

136     public void log(String JavaDoc objectName, String JavaDoc logMessage, String JavaDoc theColor)
137             throws LogException {
138         log(objectName, logMessage, theColor, ("unknown"),
139                 ("unknown"));
140     } /* log(String, String, String) */
141
142
143     /**
144      * Log a message including the uid and job number
145      *
146      * @param objectName Calling object
147      * @param theMessage Message to log
148      * @param theColor Color to log the message in
149      * @param uid User id
150      * @param jobNumber Job Number
151      */

152     public void log(String JavaDoc objectName, String JavaDoc theMessage, String JavaDoc theColor,
153                     String JavaDoc uid, String JavaDoc jobNumber)
154             throws LogException {
155         String JavaDoc myName = (thisClass + "log(String, String, String, " +
156                 "String)");
157
158         try {
159             String JavaDoc logDateTime = DateTime.getDateTimeString();
160             File JavaDoc logFile = new File JavaDoc(logFileName);
161
162             if (logFile.length() > maxBytes) {
163                 Log.clear();
164             }
165
166             FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(logFileName, true);
167             BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc(fout);
168             PrintStream JavaDoc HTMLFile = new PrintStream JavaDoc(bout);
169
170             if (theColor.equals("")) {
171                 theColor = ("white");
172             }
173             if (theColor.equalsIgnoreCase("white")) {
174                 HTMLFile.println("<tr><td nowrap>" + logDateTime +
175                         "</td><td>" + theMessage + "</td><td>" +
176                         objectName + "</td><td>" + uid + "</td><td>" +
177                         jobNumber + "</td></tr>");
178             } else if (theColor.equalsIgnoreCase("yellow")) {
179                 HTMLFile.println("<tr><td nowrap bgcolor=\"" + theColor +
180                         "\">" + logDateTime + "</td><td bgcolor=\"" +
181                         theColor + "\">" + theMessage +
182                         "</td><td bgcolor=\"" + theColor + "\">" +
183                         objectName + "</td><td bgcolor=\"" +
184                         theColor + "\">" + uid +
185                         "</td><td bgcolor=\"" + theColor + "\">" +
186                         jobNumber + "</td></tr>");
187             } else {
188                 HTMLFile.println("<tr><td nowrap bgcolor=\"" + theColor +
189                         "\"><font color=\"yellow\">" + logDateTime +
190                         "</font></td><td bgcolor=\"" + theColor +
191                         "\"><font color=\"yellow\">" + theMessage +
192                         "</font></td><td bgcolor=\"" + theColor +
193                         "\"><font color=\"yellow\">" + objectName +
194                         "</font></td><td bgcolor=\"" + theColor +
195                         "\"><font color=\"yellow\">" + uid +
196                         "</font></td><td bgcolor=\"" + theColor +
197                         "\"><font color=\"yellow\">" + jobNumber +
198                         "</font></td></tr>");
199             }
200
201             HTMLFile.flush();
202         } catch (Exception JavaDoc ioe) {
203             throw new LogException(myName + ":Exception writing log:" +
204                     ioe.getMessage(), objectName, theMessage);
205         }
206     } /* log(String, String, String, String, String) */
207
208
209     /**
210      * Log a message from an exception
211      *
212      * @param objectName Calling object
213      * @param e Throwable object to read stacktrace from
214      * @param theColor Color to log the message in
215      * @param uid User Id
216      * @param jobNumber Job Number
217      */

218     public void log(String JavaDoc objectName, Throwable JavaDoc e, String JavaDoc theColor,
219                     String JavaDoc uid, String JavaDoc jobNumber)
220             throws LogException {
221         String JavaDoc myName = (thisClass + "log(String, String, String, " +
222                 "String)");
223
224         try {
225             String JavaDoc logDateTime = DateTime.getDateTimeString();
226             File JavaDoc logFile = new File JavaDoc(logFileName);
227
228             if (logFile.length() > maxBytes) {
229                 Log.clear();
230             }
231
232             FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(logFileName, true);
233             BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc(fout);
234             PrintStream JavaDoc HTMLFile = new PrintStream JavaDoc(bout);
235
236             if (theColor.equals("")) {
237                 theColor = ("white");
238             }
239             if (theColor.equalsIgnoreCase("white")) {
240                 HTMLFile.println("<tr><td>" + logDateTime + "</td><td><pre>");
241                 e.printStackTrace(HTMLFile);
242                 HTMLFile.println("</pre></td><td>" + objectName + "</td><td>" +
243                         uid + "</td><td>" + jobNumber + "</td></tr>");
244             } else if (theColor.equalsIgnoreCase("yellow")) {
245                 HTMLFile.println("<tr><td bgcolor=\"" + theColor + "\">" +
246                         logDateTime + "</td><td bgcolor=\"" +
247                         theColor + "\"><pre>");
248                 e.printStackTrace(HTMLFile);
249                 HTMLFile.println("</pre></td><td bgcolor=\"" + theColor +
250                         "\">" + objectName + "</td><td bgcolor=\"" +
251                         theColor + "\">" + uid +
252                         "</td><td bgcolor=\"" + theColor + "\">" +
253                         jobNumber + "</td></tr>");
254             } else {
255                 HTMLFile.println("<tr><td bgcolor=\"" + theColor +
256                         "\"><font color=\"yellow\">" + logDateTime +
257                         "</font></td><td bgcolor=\"" + theColor +
258                         "\"><font color=\"yellow\"><pre>");
259                 e.printStackTrace(HTMLFile);
260                 HTMLFile.println("</pre></font></td><td bgcolor=\"" +
261                         theColor + "\"><font color=\"yellow\">" +
262                         objectName + "</font></td><td bgcolor=\"" +
263                         theColor + "\"><font color=\"yellow\">" +
264                         uid + "</font></td><td bgcolor=\"" +
265                         theColor + "\"><font color=\"yellow\">" +
266                         jobNumber + "</font></td></tr>");
267             }
268
269             HTMLFile.flush();
270         } catch (Exception JavaDoc ioe) {
271             throw new LogException(myName + ":Exception writing log:" +
272                     ioe.getMessage(), objectName,
273                     e.getMessage());
274         }
275     } /* log(String, Throwable, String, String, String) */
276
277
278     /**
279      * Log and flush the buffer to the log
280      *
281      * @param objectName Name of the calling object
282      * @param logMessage Message to log
283      * @param theColor Color to log the message
284      * @return boolean True if the message is logged successfully
285      */

286     public boolean logW(String JavaDoc objectName, String JavaDoc logMessage, String JavaDoc theColor)
287             throws LogException {
288         log(objectName, logMessage, theColor, ("unknown"),
289                 ("unknown"));
290
291         return true;
292     } /* logW(String, String, String) */
293
294
295     /**
296      * Set whether or not we echo our output to the screen (log) as well
297      *
298      * @param theFlag True if we log to standard output as well, else false
299      */

300     public synchronized void setEcho(boolean theFlag) {
301         echoOutput = theFlag;
302     } /* setEcho(boolean)
303
304     /**
305
306      * Sets the file name to write the log into
307
308      *
309
310      * @param as_filename The full pathname of the log file to write
311
312 * @throws LogException
313
314      */

315
316
317     public synchronized void setFileName(String JavaDoc as_filename)
318             throws LogException {
319         String JavaDoc myName = (thisClass + "setFileName(String)");
320
321         if (as_filename.equalsIgnoreCase("")) {
322             throw new LogException(myName + ":Empty filename not allowed");
323         }
324
325         logFileName = as_filename;
326     } /* setFileName(String) */
327
328
329 } /* HTMLLog */
330
Popular Tags