KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > log > StartupLogger


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Shai Wolf.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.core.log;
48
49 import java.util.Collection JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.LinkedList JavaDoc;
52
53 import org.apache.commons.logging.LogFactory;
54
55 /**
56  * This class is the logger used while the default logger isn't loaded.
57  * Instead of calling System.out or System.err, call one of this logger's methods.
58  * It supports the log levels: trace, debug, info, warn, error and fatal.
59  * When created, it will store all log messages until one of the following
60  * will happen:
61  * 1. The default logger is loaded - in this case all the stored messages
62  * will be forwarded to the default logger for persistency.
63  * 2. No default logger is initialized - in this case all messages will
64  * be forwarded to the standard output.
65  * After that the logger will stop storing messages. Any log request
66  * will immediately be forwarded to the default logger or standard output,
67  * depending on what happened previously.
68  *
69  * @author Shai Wolf
70  *
71  */

72 public class StartupLogger {
73     
74     // log levels
75
private static final int TRACE = 0;
76     private static final int DEBUG = 1;
77     private static final int INFO = 2;
78     private static final int WARN = 3;
79     private static final int ERROR = 4;
80     private static final int FATAL = 5;
81     
82     // an entry in the storage
83
private class StartupLoggerEntry {
84         String JavaDoc msg;
85         String JavaDoc source;
86         int logLevel;
87         
88         StartupLoggerEntry(String JavaDoc msg, String JavaDoc source, int logLevel) {
89             this.msg = msg;
90             this.source = source;
91             this.logLevel = logLevel;
92         }
93         
94         public String JavaDoc toString() {
95             return msg;
96         }
97     }
98     
99     // singleton instance
100
public static StartupLogger log = new StartupLogger();
101     
102     // Tells whether error messages arrived during manta startup
103
private boolean hasErrors = false;
104     
105     // Tells whether fatal messages arrived during manta startup
106
private boolean hasFatals = false;
107     
108     // Tells whether warning messages arrived during manta startup
109
private boolean hasWarnings = false;
110     
111     // the message storage
112
private Collection JavaDoc messages = new LinkedList JavaDoc();
113     
114     // logger mode - store or not store
115
private boolean store = false;
116     
117     // used to define the senders to each log level
118
interface Sender {
119         public void send(String JavaDoc msg, String JavaDoc loggerName);
120     }
121     
122     // list of senders - 1 sender per log level
123
private Sender[] senders;
124     
125     // private constructor for singleton
126
private StartupLogger() {
127         initSenders();
128     }
129     
130     // initializes the senders - 1 sender per log level
131
private void initSenders() {
132         senders = new Sender[6];
133         senders[TRACE] = new Sender() {
134             public void send(String JavaDoc msg, String JavaDoc source) {
135                 LogFactory.getLog(source).trace(msg);
136             }
137         };
138         senders[DEBUG] = new Sender() {
139             public void send(String JavaDoc msg, String JavaDoc source) {
140                 LogFactory.getLog(source).debug(msg);
141             }
142         };
143         senders[INFO] = new Sender() {
144             public void send(String JavaDoc msg, String JavaDoc source) {
145                 LogFactory.getLog(source).info(msg);
146             }
147         };
148         senders[WARN] = new Sender() {
149             public void send(String JavaDoc msg, String JavaDoc source) {
150                 LogFactory.getLog(source).warn(msg);
151             }
152         };
153         senders[ERROR] = new Sender() {
154             public void send(String JavaDoc msg, String JavaDoc source) {
155                 LogFactory.getLog(source).error(msg);
156             }
157         };
158         senders[FATAL] = new Sender() {
159             public void send(String JavaDoc msg, String JavaDoc source) {
160                 LogFactory.getLog(source).fatal(msg);
161             }
162         };
163     }
164     
165     // sets the logger more to store messages
166
public void startStore() {
167         this.store = true;
168     }
169     
170     // adds a new entry to the storage
171
private synchronized void log(String JavaDoc msg, String JavaDoc source, int logLevel) {
172         messages.add(new StartupLoggerEntry(msg, source, logLevel));
173     }
174     
175     // called to log a trace message
176
public synchronized void trace(String JavaDoc msg, String JavaDoc source) {
177         if (!store) {
178             LogFactory.getLog(source).trace(msg);
179             return;
180         }
181         messages.add(new StartupLoggerEntry(msg, source, TRACE));
182     }
183
184     // called to log a debug message
185
public synchronized void debug(String JavaDoc msg, String JavaDoc source) {
186         if (!store) {
187             LogFactory.getLog(source).debug(msg);
188             return;
189         }
190         messages.add(new StartupLoggerEntry(msg, source, DEBUG));
191     }
192     
193     // called to log an info message
194
public synchronized void info(String JavaDoc msg, String JavaDoc source) {
195         if (!store) {
196             LogFactory.getLog(source).info(msg);
197             return;
198         }
199         messages.add(new StartupLoggerEntry(msg, source, INFO));
200     }
201     
202     // called to log a warn message
203
public synchronized void warn(String JavaDoc msg, String JavaDoc source) {
204         if (!store) {
205             LogFactory.getLog(source).warn(msg);
206             return;
207         }
208         messages.add(new StartupLoggerEntry(msg, source, WARN));
209         hasWarnings = true;
210     }
211     
212     // called to log an error message
213
public synchronized void error(String JavaDoc msg, String JavaDoc source) {
214         if (!store) {
215             LogFactory.getLog(source).error(msg);
216             return;
217         }
218         messages.add(new StartupLoggerEntry(msg, source, ERROR));
219         hasErrors = true;
220     }
221     
222     // called to log a fatal message
223
public synchronized void fatal(String JavaDoc msg, String JavaDoc source) {
224         if (!store) {
225             LogFactory.getLog(source).fatal(msg);
226             return;
227         }
228         messages.add(new StartupLoggerEntry(msg, source, FATAL));
229         hasFatals = true;
230     }
231     
232     // returns true is error messages arrived during manta startup
233
public boolean hasErrors() {
234         return hasErrors;
235     }
236
237     // returns true is fatal messages arrived during manta startup
238
public boolean hasFatals() {
239         return hasFatals;
240     }
241     
242     // returns true is warn messages arrived during manta startup
243
public boolean hasWarnings() {
244         return hasWarnings;
245     }
246     
247     // prints the stack trace
248
private void printTrace() {
249         try {throw new Exception JavaDoc();}
250         catch (Exception JavaDoc e) {e.printStackTrace();}
251     }
252     
253     // flushes all stored messages to the default logger
254
public synchronized void flush() {
255         if (!store) {
256             printTrace();
257             return;
258         }
259         store = false;
260         StartupLoggerEntry entry;
261         Iterator JavaDoc entries = messages.iterator();
262         int size = messages.size();
263         for (int i=0 ; i<size ; i++) {
264             entry = (StartupLoggerEntry)entries.next();
265             senders[entry.logLevel].send(entry.toString(), entry.source);
266         }
267         messages.clear();
268     }
269 }
270
Popular Tags