001: /* CVS ID: $Id: ServletLogger.java,v 1.1.1.1 2002/10/02 18:42:48 wastl Exp $ */
002: package net.wastl.webmail.logger;
003:
004: import net.wastl.webmail.server.*;
005: import net.wastl.webmail.exceptions.*;
006: import net.wastl.webmail.config.*;
007:
008: /**
009: * ServletLogger.java
010: *
011: * Copyright (C) 1999-2002 Sebastian Schaffert
012: *
013: * This program is free software; you can redistribute it and/or
014: * modify it under the terms of the GNU General Public License
015: * as published by the Free Software Foundation; either version 2
016: * of the License, or (at your option) any later version.
017: *
018: * This program is distributed in the hope that it will be useful,
019: * but WITHOUT ANY WARRANTY; without even the implied warranty of
020: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: * GNU General Public License for more details.
022: *
023: * You should have received a copy of the GNU General Public License
024: * along with this program; if not, write to the Free Software
025: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
026: */
027: /**
028: * This logger implementation logs all WebMail logfile entries using the Servlet Container's
029: * log facility.
030: *
031: * @author Sebastian Schaffert
032: * @version
033: */
034: public class ServletLogger implements Logger, ConfigurationListener {
035:
036: protected WebMailServlet parent;
037: protected Storage store;
038: protected int loglevel;
039:
040: public ServletLogger(WebMailServer parent, Storage st)
041: throws WebMailException {
042: this .store = st;
043:
044: if (!(parent instanceof WebMailServlet)) {
045: throw new WebMailException(
046: "ServletLogger can only be used by a Servlet!");
047: } else {
048: this .parent = (WebMailServlet) parent;
049: }
050: parent.getConfigScheme().configRegisterIntegerKey(this ,
051: "LOGLEVEL", "5",
052: "How much debug output will be written in the logfile");
053:
054: initLog();
055: }
056:
057: protected void initLog() {
058: try {
059: loglevel = Integer.parseInt(store.getConfig("LOGLEVEL"));
060: } catch (NumberFormatException e) {
061: loglevel = 5;
062: }
063: }
064:
065: public void notifyConfigurationChange(String key) {
066: initLog();
067: }
068:
069: public void log(int level, String message) {
070: if (level <= loglevel) {
071: String s = "LEVEL " + level;
072: switch (level) {
073: case Storage.LOG_DEBUG:
074: s = "DEBUG ";
075: break;
076: case Storage.LOG_INFO:
077: s = "INFO ";
078: break;
079: case Storage.LOG_WARN:
080: s = "WARNING ";
081: break;
082: case Storage.LOG_ERR:
083: s = "ERROR ";
084: break;
085: case Storage.LOG_CRIT:
086: s = "CRITICAL";
087: break;
088: }
089: parent.getServletContext().log(s + " - " + message);
090: }
091: }
092:
093: public void log(int level, Exception ex) {
094: if (level <= loglevel) {
095: String s = "LEVEL " + level;
096: switch (level) {
097: case Storage.LOG_DEBUG:
098: s = "DEBUG ";
099: break;
100: case Storage.LOG_INFO:
101: s = "INFO ";
102: break;
103: case Storage.LOG_WARN:
104: s = "WARNING ";
105: break;
106: case Storage.LOG_ERR:
107: s = "ERROR ";
108: break;
109: case Storage.LOG_CRIT:
110: s = "CRITICAL";
111: break;
112: }
113: parent.getServletContext().log(
114: s + " - An exception occured", ex);
115: }
116: }
117:
118: public void shutdown() {
119: }
120:
121: }
|