1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LogUtil {
43  
44      public static final int STACK_TRACE_LENGTH = 20;
45  
46      public static final boolean REMOVE_UNKNOWN_SOURCE = true;
47  
48      public static void debug(Log log, Properties props) {
49          if (log.isDebugEnabled()) {
50              StringWriter sw = new StringWriter();
51  
52              props.list(new PrintWriter(sw));
53  
54              log.debug(sw.getBuffer().toString());
55          }
56      }
57  
58      public static void log(Log log, Throwable t) {
59          if (t instanceof JspException) {
60              log(log, (JspException)t);
61          }
62          else if (t instanceof ServletException) {
63              log(log, (ServletException)t);
64          }
65          else {
66              Throwable cause = t.getCause();
67  
68              if (cause != null) {
69                  log(log, cause);
70              }
71              else {
72                  _log(log, t);
73              }
74          }
75      }
76  
77      public static void log(Log log, JspException jspe) {
78          Throwable cause = jspe.getRootCause();
79  
80          if (cause == null) {
81              cause = jspe;
82          }
83  
84          if ((cause != jspe) && (cause instanceof JspException)) {
85              log(log, (JspException)cause);
86          }
87          else if (cause instanceof ServletException) {
88              log(log, (ServletException)cause);
89          }
90          else {
91              _log(log, cause);
92          }
93      }
94  
95      public static void log(Log log, ServletException se) {
96          Throwable cause = se.getRootCause();
97  
98          if (cause == null) {
99              cause = se;
100         }
101 
102         if (cause instanceof JspException) {
103             log(log, (JspException)cause);
104         }
105         else if ((cause != se) && (cause instanceof ServletException)) {
106             log(log, (ServletException)cause);
107         }
108         else {
109             _log(log, cause);
110         }
111     }
112 
113     private static void _log(Log log, Throwable cause) {
114         StackTraceElement[] steArray = cause.getStackTrace();
115 
116         // Make the stack trace more readable by limiting the number of
117         // elements.
118 
119         if (steArray.length > STACK_TRACE_LENGTH) {
120             int count = 0;
121 
122             List<StackTraceElement> steList =
123                 new ArrayList<StackTraceElement>();
124 
125             for (int i = 0; i < steArray.length; i++) {
126                 StackTraceElement ste = steArray[i];
127 
128                 // Make the stack trace more readable by removing elements that
129                 // refer to classes with no packages, or starts with a $, or are
130                 // Spring classes, or are standard reflection classes.
131 
132                 String className = ste.getClassName();
133 
134                 boolean addElement = true;
135 
136                 if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
137                     addElement = false;
138                 }
139 
140                 if (className.startsWith("$") ||
141                     className.startsWith("java.lang.reflect.") ||
142                     className.startsWith("org.springframework.") ||
143                     className.startsWith("sun.reflect.")) {
144 
145                     addElement = false;
146                 }
147 
148                 if (addElement) {
149                     steList.add(ste);
150 
151                     count++;
152                 }
153 
154                 if (count >= STACK_TRACE_LENGTH) {
155                     break;
156                 }
157             }
158 
159             steArray = steList.toArray(new StackTraceElement[steList.size()]);
160 
161             cause.setStackTrace(steArray);
162         }
163 
164         log.error(StackTraceUtil.getStackTrace(cause));
165     }
166 
167 }