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.io.unsync.UnsyncStringWriter;
26  import com.liferay.portal.kernel.util.StackTraceUtil;
27  
28  import java.io.PrintWriter;
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              UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(
51                  true, props.size() + 1);
52  
53              props.list(new PrintWriter(unsyncStringWriter));
54  
55              log.debug(unsyncStringWriter.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 }