1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.log;
21  
22  import com.liferay.portal.kernel.util.StackTraceUtil;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.jsp.JspException;
33  
34  /**
35   * <a href="LogUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class LogUtil {
41  
42      public static final int STACK_TRACE_LENGTH = 20;
43  
44      public static final boolean REMOVE_UNKNOWN_SOURCE = true;
45  
46      public static void debug(Log log, Properties props) {
47          if (log.isDebugEnabled()) {
48              StringWriter sw = new StringWriter();
49  
50              props.list(new PrintWriter(sw));
51  
52              log.debug(sw.getBuffer().toString());
53          }
54      }
55  
56      public static void log(Log log, Throwable t) {
57          if (t instanceof JspException) {
58              log(log, (JspException)t);
59          }
60          else if (t instanceof ServletException) {
61              log(log, (ServletException)t);
62          }
63          else {
64              Throwable cause = t.getCause();
65  
66              if (cause != null) {
67                  log(log, cause);
68              }
69              else {
70                  _log(log, t);
71              }
72          }
73      }
74  
75      public static void log(Log log, JspException jspe) {
76          Throwable cause = jspe.getRootCause();
77  
78          if (cause == null) {
79              cause = jspe;
80          }
81  
82          if ((cause != jspe) && (cause instanceof JspException)) {
83              log(log, (JspException)cause);
84          }
85          else if (cause instanceof ServletException) {
86              log(log, (ServletException)cause);
87          }
88          else {
89              _log(log, cause);
90          }
91      }
92  
93      public static void log(Log log, ServletException se) {
94          Throwable cause = se.getRootCause();
95  
96          if (cause == null) {
97              cause = se;
98          }
99  
100         if (cause instanceof JspException) {
101             log(log, (JspException)cause);
102         }
103         else if ((cause != se) && (cause instanceof ServletException)) {
104             log(log, (ServletException)cause);
105         }
106         else {
107             _log(log, cause);
108         }
109     }
110 
111     private static void _log(Log log, Throwable cause) {
112         StackTraceElement[] steArray = cause.getStackTrace();
113 
114         // Make the stack trace more readable by limiting the number of
115         // elements.
116 
117         if (steArray.length > STACK_TRACE_LENGTH) {
118             int count = 0;
119 
120             List<StackTraceElement> steList =
121                 new ArrayList<StackTraceElement>();
122 
123             for (int i = 0; i < steArray.length; i++) {
124                 StackTraceElement ste = steArray[i];
125 
126                 // Make the stack trace more readable by removing elements that
127                 // refer to classes with no packages, or starts with a $, or are
128                 // Spring classes, or are standard reflection classes.
129 
130                 String className = ste.getClassName();
131 
132                 boolean addElement = true;
133 
134                 if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
135                     addElement = false;
136                 }
137 
138                 if (className.startsWith("$") ||
139                     className.startsWith("java.lang.reflect.") ||
140                     className.startsWith("org.springframework.") ||
141                     className.startsWith("sun.reflect.")) {
142 
143                     addElement = false;
144                 }
145 
146                 if (addElement) {
147                     steList.add(ste);
148 
149                     count++;
150                 }
151 
152                 if (count >= STACK_TRACE_LENGTH) {
153                     break;
154                 }
155             }
156 
157             steArray = steList.toArray(new StackTraceElement[steList.size()]);
158 
159             cause.setStackTrace(steArray);
160         }
161 
162         log.error(StackTraceUtil.getStackTrace(cause));
163     }
164 
165 }