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: /**
021: * Standard implementation of the <CODE>Logger</CODE>. This is
022: * general-purpose logging facility. A client that needs additional
023: * functionality can either extend this class or provide there own
024: * implementationm of <CODE>Logger</CODE>. <P>
025: *
026: * Currently this is a bare-bones class that writes INFO and above
027: * levels to stderr and all others to a log file.
028: *
029: * @author Sinisa Milosevic
030: * @see org.webdocwf.util.loader.logging.Logger
031: */
032: public class StandardLogger extends Logger {
033:
034: /**
035: * Log file name.
036: */
037: File activeLogFile;
038:
039: /**
040: * Log dir
041: */
042: File logDir;
043:
044: /**
045: * Log file writter.
046: */
047: PrintWriter logFileStream;
048:
049: private boolean[] enabledLogLevels = new boolean[3];
050: private Hashtable messages = new Hashtable();
051: private String logMode;
052: public RandomAccessFile randomLoggerFile;
053:
054: /**
055: * Construct a new logger. Configuration is not done now, to allow
056: * the logger to be created very early.
057: */
058: public StandardLogger() {
059: centralLogger = this ;
060: this .enabledLogLevels[0] = false;
061: this .enabledLogLevels[1] = false;
062: this .enabledLogLevels[2] = false;
063: }
064:
065: /**
066: * Configure Logger with given config file, interpreting of config file is
067: * logger implementation specific.
068: *
069: * @param confFilePath Path to configuration file.
070: */
071: public void configure(String confFilePath) throws Exception {
072: int num = confFilePath.indexOf(";");
073: String logDir = confFilePath.substring(0, num);
074: String fileName = confFilePath.substring(num + 1);
075:
076: String strPi;
077: Calendar calendar = Calendar.getInstance();
078: Date currentDate = new Date();
079: calendar.setTime(currentDate);
080: int year, month, iDate, iDay, hours, minutes, seconds;
081: int y, h, min, s;
082: y = calendar.get(Calendar.YEAR);
083: month = calendar.get(Calendar.MONTH);
084: month = month + 1;
085: String strMonth = null;
086: if (month < 10)
087: strMonth = "0" + month;
088: else
089: strMonth = "" + month;
090:
091: String[] months = new String[] { "January", "February",
092: "March", "April", "May", "June", "July", "August",
093: "September", "October", "November", "December" };
094: iDate = calendar.get(Calendar.DAY_OF_MONTH);
095: String strDate = "";
096: if (iDate < 10)
097: strDate = "0" + iDate;
098: else
099: strDate = "" + iDate;
100:
101: iDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
102: String[] days = new String[] { "Sunday", "Monday", "Tuesday",
103: "Wednesday", "Thursday", "Friday", "Saturday" };
104: hours = calendar.get(Calendar.HOUR_OF_DAY);
105: String strHours = "";
106: if (hours < 10)
107: strHours = "0" + hours;
108: else
109: strHours = "" + hours;
110:
111: h = hours + 1;
112:
113: minutes = calendar.get(Calendar.MINUTE);
114: String strMinutes = "";
115: if (minutes < 10)
116: strMinutes = "0" + minutes;
117: else
118: strMinutes = "" + minutes;
119:
120: h = hours + 1;
121:
122: min = minutes + 1;
123: seconds = calendar.get(Calendar.SECOND);
124: String strSeconds = "";
125: if (seconds < 10)
126: strSeconds = "0" + seconds;
127: else
128: strSeconds = "" + seconds;
129:
130: s = seconds + 1;
131: if (fileName.equalsIgnoreCase("default")) {
132:
133: strPi = "LoaderLog" + y + "-" + strMonth + "-" + strDate
134: + "-" + strHours + "-" + strMinutes + "-"
135: + strSeconds + ".txt";
136: } else if (fileName.equalsIgnoreCase("defaultGenerator")) {
137: strPi = "GeneratorLog" + y + "-" + strMonth + "-" + strDate
138: + "-" + strHours + "-" + strMinutes + "-"
139: + strSeconds + ".txt";
140: } else
141: strPi = fileName;
142: try {
143: File filea = new File(logDir);
144: this .logDir = filea;
145: // if (!filea.exists())
146: // filea.mkdirs();
147: filea = new File(filea.getAbsolutePath(), strPi);
148: // randomLoggerFile = new RandomAccessFile(filea, "rw");
149: this .activeLogFile = filea.getAbsoluteFile();
150: // write("normal", "Created: " + strPi);
151: // randomLoggerFile.writeBytes("Date: " + days[iDay] + ", " + iDate + ". " +
152: // months[month - 1] + " " + y + ".\n" + "Time: " + hours + ":"
153: // + minutes + ":" + seconds + "\n");
154:
155: } catch (Exception ex) {
156: throw new Exception("Cannot configure StandardLogger:"
157: + ex.getMessage());
158: }
159:
160: }
161:
162: public int getLevel(String level) {
163: if (level.equalsIgnoreCase(Logger.strLOGMODE_NONE))
164: return 0;
165: else if (level.equalsIgnoreCase(Logger.strLOGMODE_NORMAL))
166: return 1;
167: else
168: return 2;
169: }
170:
171: public boolean isEnabled(int level) {
172: boolean[] enabledLevelsValue = this .getEnabledLogLevels();
173: if (enabledLevelsValue[level] == true) {
174: return true;
175: }
176: return false;
177: }
178:
179: public boolean isEnabled(String level) {
180: return isEnabled(this .getLevel(level));
181: }
182:
183: public void write(int level, String msg) {
184: if (isEnabled(level)) {
185: try {
186: if (this .activeLogFile != null) {
187: //create directory structure
188: if (!this .logDir.exists())
189: this .logDir.mkdirs();
190: //create random access file
191: if (this .randomLoggerFile == null)
192: this .randomLoggerFile = new RandomAccessFile(
193: this .activeLogFile, "rw");
194: //write
195: randomLoggerFile.seek(randomLoggerFile.length());
196: System.out.println(msg + "\n");
197: BufferOctopusClass.getInstance().writeToBuffer(
198: msg + "\n");
199: randomLoggerFile.writeBytes(msg + "\n");
200: }
201: } catch (Exception e) {
202: BufferOctopusClass.getInstance().writeToBuffer(
203: e.getMessage() + "\n");
204: e.printStackTrace();
205: }
206: }
207: }
208:
209: public synchronized void write(String level, String msg) {
210: write(getLevel(level), msg);
211: }
212:
213: public synchronized void write(int level, String msg,
214: Throwable throwable) {
215: if (isEnabled(level)) {
216: Date date = new Date();
217: StringWriter stackBuf = new StringWriter();
218: throwable.printStackTrace(new PrintWriter(stackBuf));
219: stackBuf.flush();
220:
221: String errMsg = msg + ":" + " " + throwable.getMessage()
222: + '\n' + stackBuf;
223: this .write(level, errMsg);
224: }
225: }
226:
227: public synchronized void write(String level, String msg,
228: Throwable throwable) {
229: write(getLevel(level), msg, throwable);
230: }
231:
232: public void setEnabledLogLevels(String logMode) {
233: this .logMode = logMode;
234: int level = this .getLevel(logMode);
235: if (level == 0) {
236: this .enabledLogLevels[0] = false;
237: this .enabledLogLevels[1] = false;
238: this .enabledLogLevels[2] = false;
239: } else if (level == 1) {
240: this .enabledLogLevels[0] = true;
241: this .enabledLogLevels[1] = true;
242: this .enabledLogLevels[2] = false;
243: } else {
244: this .enabledLogLevels[0] = true;
245: this .enabledLogLevels[1] = true;
246: this .enabledLogLevels[2] = true;
247: }
248: }
249:
250: public boolean[] getEnabledLogLevels() {
251: return enabledLogLevels;
252: }
253:
254: public String getMessage(String key) {
255: if (key != null) {
256: return (String) this .messages.get(key);
257: } else
258: return null;
259: }
260:
261: public boolean setMessage(String key, String value) {
262: if (value != null && key != null) {
263: this .messages.put(key, value);
264: return true;
265: } else
266: return false;
267: }
268:
269: public boolean writeEcho(String strLogTxt) {
270: if (!this .logMode.equalsIgnoreCase(Logger.strLOGMODE_NONE)) {
271: this .write(Logger.strLOGMODE_NORMAL, strLogTxt);
272: BufferOctopusClass.getInstance().writeToBuffer(strLogTxt);
273: return true;
274: } else
275: return false;
276: }
277:
278: public void close() {
279: try {
280: if (this .activeLogFile != null)
281: this .activeLogFile = null;
282: this .randomLoggerFile.close();
283: } catch (Exception e) {
284: BufferOctopusClass.getInstance().writeToBuffer(
285: e.getMessage());
286: }
287: }
288:
289: }
|