001: /**
002: Copyright (C) 2002-2003 Together
003: This library is free software; you can redistribute it and/or
004: modify it under the terms of the GNU Lesser General Public
005: License as published by the Free Software Foundation; either
006: version 2.1 of the License, or (at your option) any later version.
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010: Lesser General Public License for more details.
011: You should have received a copy of the GNU Lesser General Public
012: License along with this library; if not, write to the Free Software
013: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111--1307 USA
014: */package org.webdocwf.util.loader.logging;
015:
016: import java.util.*;
017: import java.io.*;
018: import org.webdocwf.util.loader.BufferOctopusClass;
019:
020: import java.util.Hashtable;
021: import java.io.*;
022: import org.apache.log4j.BasicConfigurator;
023: import org.apache.log4j.ConsoleAppender;
024: import org.apache.log4j.Level;
025: import org.apache.log4j.Logger;
026: import org.apache.log4j.PatternLayout;
027: import org.apache.log4j.RollingFileAppender;
028: import org.apache.log4j.spi.LoggerRepository;
029: import org.apache.log4j.xml.DOMConfigurator;
030:
031: public class Log4jLogger extends
032: org.webdocwf.util.loader.logging.Logger {
033:
034: /**
035: * Log file name.
036: */
037: File activeLogFile;
038:
039: /**
040: * Log file writter.
041: */
042: PrintWriter logFileStream;
043:
044: private boolean[] enabledLogLevels = new boolean[3];
045: private Hashtable messages = new Hashtable();
046: private String logMode;
047: private org.apache.log4j.Logger logger;
048:
049: /**
050: * Construct a new logger. Configuration is not done now, to allow
051: * the logger to be created very early.
052: */
053: public Log4jLogger() {
054: centralLogger = this ;
055: }
056:
057: /**
058: * Configure Logger with given config file, interpreting of config file is
059: * logger implementation specific.
060: *
061: * @param log4jConfFile is Path to configuration file.
062: * @throws Exception
063: */
064: public void configure(String log4jConfFile) throws Exception {
065:
066: try {
067: DOMConfigurator.configure(log4jConfFile);
068: this .logger = Logger.getLogger("OctopusLogger");
069: } catch (javax.xml.parsers.FactoryConfigurationError fce) {
070: throw new Exception(
071: "Cannot configure Log4jLogger! FactoryConfigurationError occured!");
072:
073: }
074: }
075:
076: public int getLevel(String level) {
077: if (level
078: .equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE))
079: return 0;
080: else if (level
081: .equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL))
082: return 1;
083: else
084: return 2;
085: }
086:
087: public boolean isEnabled(int level) {
088: return true;
089: }
090:
091: public boolean isEnabled(String level) {
092: // return isEnabled(this.getLevel(level));
093: return true;
094: }
095:
096: public void write(int level, String msg) {
097: Level lev;
098: if (this .logger == null) {
099: this .logger = Logger.getLogger("OctopusLogger");
100: }
101: if (level == 1)
102: lev = Level.INFO;
103: else if (level == 2)
104: lev = Level.DEBUG;
105: else
106: lev = Level.INFO;
107:
108: logger.log(lev, msg);
109: }
110:
111: public synchronized void write(String level, String msg) {
112: write(getLevel(level), msg);
113: }
114:
115: public synchronized void write(int level, String msg,
116: Throwable throwable) {
117: Date date = new Date();
118: StringWriter stackBuf = new StringWriter();
119: throwable.printStackTrace(new PrintWriter(stackBuf));
120: stackBuf.flush();
121:
122: String errMsg = msg + ":" + " " + throwable.getMessage() + '\n'
123: + stackBuf;
124: this .write(level, errMsg);
125:
126: }
127:
128: public synchronized void write(String level, String msg,
129: Throwable throwable) {
130: write(getLevel(level), msg, throwable);
131: }
132:
133: public void setEnabledLogLevels(String logMode) {
134: }
135:
136: public boolean[] getEnabledLogLevels() {
137: return enabledLogLevels;
138: }
139:
140: public String getMessage(String key) {
141: if (key != null) {
142: return (String) this .messages.get(key);
143: } else
144: return null;
145: }
146:
147: public boolean setMessage(String key, String value) {
148: if (value != null && key != null) {
149: this .messages.put(key, value);
150: return true;
151: } else
152: return false;
153: }
154:
155: public boolean writeEcho(String strLogTxt) {
156: if (!this .logMode
157: .equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE)) {
158: this
159: .write(
160: org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL,
161: strLogTxt);
162: BufferOctopusClass.getInstance().writeToBuffer(strLogTxt);
163: return true;
164: } else
165: return false;
166: }
167:
168: public void close() {
169: //
170: }
171: }
|