1
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
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
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
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 }