1
19
20 package com.liferay.portal.kernel.log;
21
22 import com.liferay.portal.kernel.util.StackTraceUtil;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Properties;
30
31 import javax.servlet.ServletException;
32 import javax.servlet.jsp.JspException;
33
34
40 public class LogUtil {
41
42 public static final int STACK_TRACE_LENGTH = 20;
43
44 public static final boolean REMOVE_UNKNOWN_SOURCE = true;
45
46 public static void debug(Log log, Properties props) {
47 if (log.isDebugEnabled()) {
48 StringWriter sw = new StringWriter();
49
50 props.list(new PrintWriter(sw));
51
52 log.debug(sw.getBuffer().toString());
53 }
54 }
55
56 public static void log(Log log, Throwable t) {
57 if (t instanceof JspException) {
58 log(log, (JspException)t);
59 }
60 else if (t instanceof ServletException) {
61 log(log, (ServletException)t);
62 }
63 else {
64 Throwable cause = t.getCause();
65
66 if (cause != null) {
67 log(log, cause);
68 }
69 else {
70 _log(log, t);
71 }
72 }
73 }
74
75 public static void log(Log log, JspException jspe) {
76 Throwable cause = jspe.getRootCause();
77
78 if (cause == null) {
79 cause = jspe;
80 }
81
82 if ((cause != jspe) && (cause instanceof JspException)) {
83 log(log, (JspException)cause);
84 }
85 else if (cause instanceof ServletException) {
86 log(log, (ServletException)cause);
87 }
88 else {
89 _log(log, cause);
90 }
91 }
92
93 public static void log(Log log, ServletException se) {
94 Throwable cause = se.getRootCause();
95
96 if (cause == null) {
97 cause = se;
98 }
99
100 if (cause instanceof JspException) {
101 log(log, (JspException)cause);
102 }
103 else if ((cause != se) && (cause instanceof ServletException)) {
104 log(log, (ServletException)cause);
105 }
106 else {
107 _log(log, cause);
108 }
109 }
110
111 private static void _log(Log log, Throwable cause) {
112 StackTraceElement[] steArray = cause.getStackTrace();
113
114
117 if (steArray.length > STACK_TRACE_LENGTH) {
118 int count = 0;
119
120 List<StackTraceElement> steList =
121 new ArrayList<StackTraceElement>();
122
123 for (int i = 0; i < steArray.length; i++) {
124 StackTraceElement ste = steArray[i];
125
126
130 String className = ste.getClassName();
131
132 boolean addElement = true;
133
134 if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
135 addElement = false;
136 }
137
138 if (className.startsWith("$") ||
139 className.startsWith("java.lang.reflect.") ||
140 className.startsWith("org.springframework.") ||
141 className.startsWith("sun.reflect.")) {
142
143 addElement = false;
144 }
145
146 if (addElement) {
147 steList.add(ste);
148
149 count++;
150 }
151
152 if (count >= STACK_TRACE_LENGTH) {
153 break;
154 }
155 }
156
157 steArray = steList.toArray(new StackTraceElement[steList.size()]);
158
159 cause.setStackTrace(steArray);
160 }
161
162 log.error(StackTraceUtil.getStackTrace(cause));
163 }
164
165 }