001: /* CVS ID: $Id: FileLogger.java,v 1.1.1.1 2002/10/02 18:42:48 wastl Exp $ */
002: package net.wastl.webmail.logger;
003:
004: import java.io.*;
005: import java.util.*;
006: import java.text.*;
007: import net.wastl.webmail.server.*;
008: import net.wastl.webmail.misc.*;
009: import net.wastl.webmail.config.*;
010:
011: /**
012: * Logger.java
013: *
014: * Created: Sun Sep 19 18:58:28 1999
015: *
016: * Copyright (C) 1999-2000 Sebastian Schaffert
017: *
018: * This program is free software; you can redistribute it and/or
019: * modify it under the terms of the GNU General Public License
020: * as published by the Free Software Foundation; either version 2
021: * of the License, or (at your option) any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
031: */
032: /**
033: * This is an asynchronous Logger thread that accepts log messages to a Queue and writes
034: * them to the logfile from time to time (all 5 seconds).
035: *
036: * @author Sebastian Schaffert
037: * @version
038: */
039: public class FileLogger extends Thread implements
040: ConfigurationListener, Logger {
041:
042: private DateFormat df = null;
043:
044: protected PrintWriter logout;
045: protected int loglevel;
046:
047: protected Queue queue;
048: protected Queue time_queue;
049:
050: protected boolean do_shutdown = false;
051:
052: protected WebMailServer parent;
053: protected Storage store;
054:
055: protected int interval;
056:
057: public FileLogger(WebMailServer parent, Storage st) {
058: super ("FileLogger Thread");
059: this .parent = parent;
060: this .store = st;
061: parent.getConfigScheme().configRegisterIntegerKey(this ,
062: "LOGLEVEL", "5",
063: "How much debug output will be written in the logfile");
064: parent.getConfigScheme().configRegisterStringKey(
065: this ,
066: "LOGFILE",
067: parent.getProperty("webmail.data.path")
068: + System.getProperty("file.separator")
069: + "webmail.log", "WebMail logfile");
070: parent
071: .getConfigScheme()
072: .configRegisterIntegerKey(
073: this ,
074: "LOG INTERVAL",
075: "5",
076: "Interval used for flushing the log buffer"
077: + " in seconds. Log messages of level CRIT or"
078: + " ERR will be written immediately in any way.");
079: initLog();
080: queue = new Queue();
081: time_queue = new Queue();
082: this .start();
083: }
084:
085: protected void initLog() {
086: System.err.print(" * Logfile ... ");
087: try {
088: loglevel = Integer.parseInt(store.getConfig("LOGLEVEL"));
089: } catch (NumberFormatException e) {
090: }
091: try {
092: interval = Integer
093: .parseInt(store.getConfig("LOG INTERVAL"));
094: } catch (NumberFormatException e) {
095: }
096: try {
097: String filename = store.getConfig("LOGFILE");
098: logout = new PrintWriter(new FileOutputStream(filename,
099: true));
100: System.err.println("initalization complete: " + filename
101: + ", Level " + loglevel);
102: } catch (IOException ex) {
103: ex.printStackTrace();
104: logout = new PrintWriter(System.out);
105: System.err
106: .println("initalization complete: Sending to STDOUT, Level "
107: + loglevel);
108: }
109: }
110:
111: protected String formatDate(long date) {
112: if (df == null) {
113: TimeZone tz = TimeZone.getDefault();
114: df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
115: DateFormat.DEFAULT, Locale.getDefault());
116: df.setTimeZone(tz);
117: }
118: String now = df.format(new Date(date));
119: return now;
120: }
121:
122: protected void writeMessage(long time, String message) {
123: logout.println("[" + formatDate(time) + "] - " + message);
124: }
125:
126: public void log(int level, String message) {
127: if (level <= loglevel) {
128: String s = "LEVEL " + level;
129: switch (level) {
130: case Storage.LOG_DEBUG:
131: s = "DEBUG ";
132: break;
133: case Storage.LOG_INFO:
134: s = "INFO ";
135: break;
136: case Storage.LOG_WARN:
137: s = "WARNING ";
138: break;
139: case Storage.LOG_ERR:
140: s = "ERROR ";
141: break;
142: case Storage.LOG_CRIT:
143: s = "CRITICAL";
144: break;
145: }
146: queue(System.currentTimeMillis(), s + " - " + message);
147: if (level <= Storage.LOG_ERR) {
148: flush();
149: }
150: }
151: }
152:
153: public void log(int level, Exception ex) {
154: if (level <= loglevel) {
155: String s = "LEVEL " + level;
156: switch (level) {
157: case Storage.LOG_DEBUG:
158: s = "DEBUG ";
159: break;
160: case Storage.LOG_INFO:
161: s = "INFO ";
162: break;
163: case Storage.LOG_WARN:
164: s = "WARNING ";
165: break;
166: case Storage.LOG_ERR:
167: s = "ERROR ";
168: break;
169: case Storage.LOG_CRIT:
170: s = "CRITICAL";
171: break;
172: }
173: StringWriter message = new StringWriter();
174: ex.printStackTrace(new PrintWriter(message));
175: queue(System.currentTimeMillis(), s + " - "
176: + message.toString());
177: if (level <= Storage.LOG_ERR) {
178: flush();
179: }
180: }
181: }
182:
183: protected void flush() {
184: while (!queue.isEmpty()) {
185: Long l = (Long) time_queue.next();
186: String s = (String) queue.next();
187: writeMessage(l.longValue(), s);
188: }
189: logout.flush();
190: }
191:
192: public void queue(long time, String message) {
193: queue.queue(message);
194: time_queue.queue(new Long(time));
195: }
196:
197: public void notifyConfigurationChange(String key) {
198: initLog();
199: }
200:
201: public void shutdown() {
202: flush();
203: do_shutdown = true;
204: }
205:
206: public void run() {
207: while (!do_shutdown) {
208: flush();
209: try {
210: sleep(interval * 1000);
211: } catch (InterruptedException e) {
212: }
213: }
214: }
215: } // FileLogger
|