001: /*
002: * ErrorLog.java
003: *
004: *
005: * Copyright (c) 2004 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.webserver;
054:
055: import java.io.*;
056: import java.util.*;
057: import java.text.*;
058:
059: /**
060: *
061: * @author lars
062: */
063: public class ErrorLog {
064: private static BufferedWriter bw = null;
065: private static FileWriter fw;
066:
067: private static String logFileName = "log/error.log";
068: private static long logFileSize = 100 * 1024;
069:
070: private final static int numberOfEntries = 100;
071:
072: private static String[] lastEntries = new String[numberOfEntries];
073: private static int index = 0;
074: private static boolean firstTime = true;
075: private static int firstEntry = 0;
076:
077: /** Creates a new instance of ErrorLog */
078: public ErrorLog() {
079: }
080:
081: public static void setLogFile(String name) {
082: logFileName = name;
083: }
084:
085: private static void newLogFile() {
086: try {
087: fw = new FileWriter(logFileName, true); // append to old file
088: bw = new BufferedWriter(fw);
089: } catch (Exception e) {
090: String str = new String(DateFormat.getDateTimeInstance()
091: .format(new Date())
092: + "Exception occured in Log.write: " + e);
093: try {
094: bw.write(str, 0, str.length());
095: bw.newLine();
096: } catch (Exception ex) {
097: }
098: }
099: }
100:
101: private static void checkFile() {
102: // Check if the stream is open, if not create a new stream.
103: if (bw == null)
104: newLogFile();
105:
106: java.io.File logFile = new java.io.File(logFileName);
107:
108: // If the log file is greater than logFileSize, rename it to "logfile.old"
109: // and create a new log file.
110: try {
111: if (logFile.length() > logFileSize) {
112:
113: java.io.File oldLogFile = new java.io.File(logFileName
114: + "_"
115: + DateFormat.getDateTimeInstance().format(
116: new Date()) + ".old");
117:
118: if (oldLogFile.exists())
119: oldLogFile.delete();
120: bw.close(); // closes the current stream
121: bw = null;
122: if (logFile.renameTo(oldLogFile) != true) {
123: String str = new String(DateFormat
124: .getDateTimeInstance().format(new Date())
125: + "Error, can't rename to " + oldLogFile);
126: System.err.println(str);
127: }
128: newLogFile();
129: }
130: } catch (Exception e) {
131: //e.printStackTrace();
132: }
133: }
134:
135: private static synchronized void write(String logString) {
136: try {
137: checkFile();
138: bw.write(logString, 0, logString.length());
139: bw.newLine();
140: bw.flush();
141: lastEntries[index] = logString;
142: if (!firstTime) {
143: if (index == numberOfEntries - 1)
144: firstEntry = 0;
145: else
146: firstEntry = index + 1;
147: }
148:
149: index++;
150: if (index >= numberOfEntries) {
151: firstTime = false;
152: index = 0;
153: }
154:
155: } catch (IOException e) {
156: } catch (Exception e) {
157: }
158: }
159:
160: private static String getMiliSec() {
161: String t = Long.toString(new Date().getTime());
162: return ("." + t.substring((t.length() - 3), t.length()));
163: }
164:
165: public static void log(String class_name, int linenumber,
166: String error, String description) {
167: com.rimfaxe.util.Calendar cal = new com.rimfaxe.util.Calendar();
168: write("\n"
169: + "----------------------------------------------------------------------------------\n"
170: + ""
171: + cal.getTimestamp()
172: + getMiliSec()
173: + "\n"
174: + ""
175: + class_name
176: + "["
177: + linenumber
178: + "]"
179: + "\n"
180: + ""
181: + error
182: + "\n"
183: + ""
184: + description
185: + "\n"
186: + "==================================================================================\n\n");
187: }
188:
189: }
|