KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DBAppender.java
67  *
68  * Copyright 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.logging;
71
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
74 import com.jcorporate.expresso.services.dbobj.LogEntry;
75 import org.apache.log4j.AppenderSkeleton;
76 import org.apache.log4j.Priority;
77 import org.apache.log4j.helpers.LogLog;
78 import org.apache.log4j.spi.LoggingEvent;
79
80 import java.io.ByteArrayOutputStream JavaDoc;
81 import java.io.PrintStream JavaDoc;
82
83
84 /**
85  * JDBC type appender for Log4j
86  *
87  * @author Michael Rimov
88  */

89 public class DBAppender
90         extends AppenderSkeleton {
91     private static final int ERROR = Priority.ERROR.toInt();
92     private static final int WARN = Priority.WARN.toInt();
93     private static final int INFO = Priority.INFO.toInt();
94     private static final int DEBUG = Priority.DEBUG.toInt();
95     private static final String JavaDoc thisClass = "com.jcorporate.expresso.core.logging.DBAppender.";
96
97     /**
98      *
99      */

100     public DBAppender() {
101     } /* DBAppender() */
102
103     /**
104      * @return true all the time
105      */

106     public boolean requiresLayout() {
107         return true;
108     } /* requiresLayout() */
109
110     /**
111      * Uses Expresso's Log Handler.
112      *
113      * @param parm1 no longer used
114      */

115     protected void append(LoggingEvent parm1) {
116         final String JavaDoc myName = "com.jcorporate.expresso.core.logging.DBAppender";
117         String JavaDoc theMessage = layout.format(parm1);
118         String JavaDoc theColor = "R";
119
120         //Determine Color based upon the priority of the message.
121
int priorityInt = parm1.getLevel().toInt();
122
123
124
125         //Filter the possible priorities into the standard four colors
126
if (priorityInt <= DEBUG) {
127             theColor = "G";
128         } else if (priorityInt <= INFO) {
129             theColor = "B";
130         } else if (priorityInt <= WARN) {
131             theColor = "Y";
132         } else if (priorityInt <= ERROR) {
133             theColor = "R";
134         }
135
136         //Drop the values by 1000 to make it jive with 0-9
137
priorityInt /= 10000;
138
139         try {
140
141             //Put the appender into the db setup
142
log(priorityInt, parm1.getLoggerName(), theMessage, theColor);
143
144             //
145
//Check if there's a possibility of throwing an event.
146
//
147
if (layout.ignoresThrowable() &&
148                     parm1.getThrowableInformation() != null) {
149                 log(parm1.getLoggerName(),
150                         parm1.getThrowableInformation().getThrowable());
151             }
152         } catch (LogException e) {
153
154             //TODO: Maybe need to use an Error Handler Class Instead?????
155
LogLog.error(myName + ":Unable to log message '" + theMessage +
156                     "' from category '" + parm1.getLoggerName() + "':", e);
157         }
158     } /* append(LoggingEvent) */
159
160     /**
161      * Log an exception from a particular object
162      *
163      * @param objectName Calling object
164      * @param e Exception to log
165      * @throws LogException upon error
166      */

167     protected static void log(String JavaDoc objectName, Throwable JavaDoc e)
168             throws LogException {
169         String JavaDoc myName = (thisClass + "log(String, Exception)");
170         String JavaDoc message = e.getMessage();
171
172         if (e instanceof DBException) {
173             DBException de = (DBException) e;
174             message = de.getMessage() + ":" + de.getDBMessage();
175         }
176         try {
177             LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
178             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
179             e.printStackTrace(new PrintStream JavaDoc(bos));
180             myLog.setField("MessageText", bos.toString());
181             myLog.setField("MessageColor", "R");
182             myLog.setField("ObjectName", objectName);
183             LogHandler.staticAddToQueue(myLog);
184         } catch (DBException de) {
185             throw new LogException(myName + ":" + de.getMessage());
186         }
187     } /* log(String, Throwable) */
188
189
190     /**
191      * Log a message from a particular object with a color at the given
192      * level
193      *
194      * @param messageLevel Message level to log
195      * @param objectName Calling object
196      * @param msg Message to log
197      * @param color Color to log the message with. One character 'R' 'G' 'B' or 'Y'
198      * @throws LogException if the message cannot be logged
199      */

200     protected static void log(int messageLevel, String JavaDoc objectName, String JavaDoc msg,
201                               String JavaDoc color)
202             throws LogException {
203         String JavaDoc myName = (thisClass + "log(int, String, String, " +
204                 "String)");
205
206         try {
207             LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT);
208             myLog.setField("ObjectName", objectName);
209             myLog.setField("MessageText", msg);
210             myLog.setField("MessageColor", color);
211             myLog.setField("MessageLevel", "" + messageLevel);
212             myLog.setTimeStamp();
213             LogHandler.staticAddToQueue(myLog);
214         } catch (DBException de) {
215
216             //Caught an exception. Log it to the loglog facility.
217
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
218             de.printStackTrace(new PrintStream JavaDoc(bos));
219             LogLog.error(myName + ":Unable to log '" + msg + "'" +
220                     bos.toString());
221         }
222     } /* log(int, String, String, String) */
223
224
225     /**
226      *
227      */

228     public void close() {
229         final String JavaDoc myName = thisClass + "close()";
230
231         try {
232             LogHandler.flush();
233         } catch (LogException e) {
234
235             //Caught an exception. Log it to the loglog facility.
236
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
237             e.printStackTrace(new PrintStream JavaDoc(bos));
238             LogLog.error(myName + ":Unable to flush LogHandler '" +
239                     e.getMessage() + "'" + bos.toString());
240         }
241     } /* close() */
242
243 } /* DBAppender */
244
Popular Tags