1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.log;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
18  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
19  import com.liferay.portal.kernel.util.StackTraceUtil;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Properties;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.jsp.JspException;
27  
28  /**
29   * <a href="LogUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class LogUtil {
34  
35      public static final int STACK_TRACE_LENGTH = 20;
36  
37      public static final boolean REMOVE_UNKNOWN_SOURCE = true;
38  
39      public static void debug(Log log, Properties props) {
40          if (log.isDebugEnabled()) {
41              UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(
42                  props.size() + 1);
43  
44              props.list(new UnsyncPrintWriter(unsyncStringWriter));
45  
46              log.debug(unsyncStringWriter.toString());
47          }
48      }
49  
50      public static void log(Log log, Throwable t) {
51          if (t instanceof JspException) {
52              log(log, (JspException)t);
53          }
54          else if (t instanceof ServletException) {
55              log(log, (ServletException)t);
56          }
57          else {
58              Throwable cause = t.getCause();
59  
60              if (cause != null) {
61                  log(log, cause);
62              }
63              else {
64                  _log(log, t);
65              }
66          }
67      }
68  
69      public static void log(Log log, JspException jspe) {
70          Throwable cause = jspe.getCause();
71  
72          if (cause == null) {
73              cause = jspe;
74          }
75  
76          if ((cause != jspe) && (cause instanceof JspException)) {
77              log(log, (JspException)cause);
78          }
79          else if (cause instanceof ServletException) {
80              log(log, (ServletException)cause);
81          }
82          else {
83              _log(log, cause);
84          }
85      }
86  
87      public static void log(Log log, ServletException se) {
88          Throwable cause = se.getRootCause();
89  
90          if (cause == null) {
91              cause = se;
92          }
93  
94          if (cause instanceof JspException) {
95              log(log, (JspException)cause);
96          }
97          else if ((cause != se) && (cause instanceof ServletException)) {
98              log(log, (ServletException)cause);
99          }
100         else {
101             _log(log, cause);
102         }
103     }
104 
105     private static void _log(Log log, Throwable cause) {
106         StackTraceElement[] steArray = cause.getStackTrace();
107 
108         // Make the stack trace more readable by limiting the number of
109         // elements.
110 
111         if (steArray.length > STACK_TRACE_LENGTH) {
112             int count = 0;
113 
114             List<StackTraceElement> steList =
115                 new ArrayList<StackTraceElement>();
116 
117             for (int i = 0; i < steArray.length; i++) {
118                 StackTraceElement ste = steArray[i];
119 
120                 // Make the stack trace more readable by removing elements that
121                 // refer to classes with no packages, or starts with a $, or are
122                 // Spring classes, or are standard reflection classes.
123 
124                 String className = ste.getClassName();
125 
126                 boolean addElement = true;
127 
128                 if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
129                     addElement = false;
130                 }
131 
132                 if (className.startsWith("$") ||
133                     className.startsWith("java.lang.reflect.") ||
134                     className.startsWith("org.springframework.") ||
135                     className.startsWith("sun.reflect.")) {
136 
137                     addElement = false;
138                 }
139 
140                 if (addElement) {
141                     steList.add(ste);
142 
143                     count++;
144                 }
145 
146                 if (count >= STACK_TRACE_LENGTH) {
147                     break;
148                 }
149             }
150 
151             steArray = steList.toArray(new StackTraceElement[steList.size()]);
152 
153             cause.setStackTrace(steArray);
154         }
155 
156         log.error(StackTraceUtil.getStackTrace(cause));
157     }
158 
159 }