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