001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.http.log;
030:
031: import com.caucho.util.Alarm;
032: import com.caucho.util.CharBuffer;
033: import com.caucho.util.CompileException;
034: import com.caucho.util.ExceptionWrapper;
035: import com.caucho.util.QDate;
036: import com.caucho.vfs.WriteStream;
037:
038: import javax.servlet.ServletContext;
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043:
044: /**
045: * Represents an log of every error log request to the server.
046: */
047: public class ErrorLog extends AbstractErrorLog {
048: /**
049: * Logs an error.
050: *
051: * @param message the error message
052: * @param request the servlet request
053: * @param response the servlet response
054: * @param application the servlet context
055: */
056: public void log(String message, HttpServletRequest request,
057: HttpServletResponse response, ServletContext application)
058: throws IOException {
059: WriteStream logStream = getLogStream();
060:
061: if (logStream == null)
062: return;
063:
064: CharBuffer cb = CharBuffer.allocate();
065:
066: QDate.formatLocal(cb, Alarm.getCurrentTime(),
067: "[%Y/%m/%d %H:%M:%S] ");
068:
069: cb.append(message);
070:
071: logStream.log(cb.close());
072:
073: logStream.flush();
074: }
075:
076: /**
077: * Logs a message to the error log.
078: *
079: * @param log the error log to write the message.
080: * @param message the message to write
081: * @param e the exception to write
082: */
083: public void log(String message, Throwable e,
084: HttpServletRequest request, HttpServletResponse response,
085: ServletContext application) throws IOException {
086: WriteStream logStream = getLogStream();
087:
088: if (logStream == null)
089: return;
090:
091: Throwable t = e;
092: while (t != null) {
093: e = t;
094:
095: if (e instanceof ServletException)
096: t = ((ServletException) e).getRootCause();
097: else if (e instanceof ExceptionWrapper)
098: t = ((ExceptionWrapper) e).getRootCause();
099: else
100: t = null;
101: }
102:
103: CharBuffer cb = CharBuffer.allocate();
104:
105: QDate.formatLocal(cb, Alarm.getCurrentTime(),
106: "[%Y/%m/%d %H:%M:%S] ");
107:
108: cb.append(message);
109:
110: logStream.log(cb.close());
111:
112: if (e != null && !(e instanceof CompileException))
113: logStream.log(e);
114:
115: logStream.flush();
116: }
117:
118: /**
119: * Cleanup the log.
120: */
121: public void destroy() throws IOException {
122: }
123: }
|