KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > http > log > ErrorLog


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.http.log;
30
31 import com.caucho.util.Alarm;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.CompileException;
34 import com.caucho.util.ExceptionWrapper;
35 import com.caucho.util.QDate;
36 import com.caucho.vfs.WriteStream;
37
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 /**
45  * Represents an log of every error log request to the server.
46  */

47 public class ErrorLog extends AbstractErrorLog {
48   /**
49    * Logs an error.
50    *
51    * @param message the error message
52    * @param request the servlet request
53    * @param response the servlet response
54    * @param application the servlet context
55    */

56   public void log(String JavaDoc message,
57                   HttpServletRequest JavaDoc request,
58                   HttpServletResponse JavaDoc response,
59                   ServletContext JavaDoc application)
60     throws IOException JavaDoc
61   {
62     WriteStream logStream = getLogStream();
63
64     if (logStream == null)
65       return;
66
67     CharBuffer cb = CharBuffer.allocate();
68
69     QDate.formatLocal(cb, Alarm.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");
70
71     cb.append(message);
72
73     logStream.log(cb.close());
74
75     logStream.flush();
76   }
77
78   /**
79    * Logs a message to the error log.
80    *
81    * @param log the error log to write the message.
82    * @param message the message to write
83    * @param e the exception to write
84    */

85   public void log(String JavaDoc message,
86                   Throwable JavaDoc e,
87                   HttpServletRequest JavaDoc request,
88                   HttpServletResponse JavaDoc response,
89                   ServletContext JavaDoc application)
90     throws IOException JavaDoc
91   {
92     WriteStream logStream = getLogStream();
93
94     if (logStream == null)
95       return;
96
97     Throwable JavaDoc t = e;
98     while (t != null) {
99       e = t;
100         
101       if (e instanceof ServletException JavaDoc)
102         t = ((ServletException JavaDoc) e).getRootCause();
103       else if (e instanceof ExceptionWrapper)
104         t = ((ExceptionWrapper) e).getRootCause();
105       else
106         t = null;
107     }
108
109     CharBuffer cb = CharBuffer.allocate();
110
111     QDate.formatLocal(cb, Alarm.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");
112
113     cb.append(message);
114
115     logStream.log(cb.close());
116
117     if (e != null && ! (e instanceof CompileException))
118       logStream.log(e);
119
120     logStream.flush();
121   }
122
123   /**
124    * Cleanup the log.
125    */

126   public void destroy()
127     throws IOException JavaDoc
128   {
129   }
130 }
131
Popular Tags