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
43 public class LogUtil {
44
45 public static final int STACK_TRACE_LENGTH = 20;
46
47 public static final boolean REMOVE_UNKNOWN_SOURCE = true;
48
49 public static void debug(Log log, Properties props) {
50 if (log.isDebugEnabled()) {
51 StringWriter sw = new StringWriter();
52
53 props.list(new PrintWriter(sw));
54
55 log.debug(sw.getBuffer().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
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
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 }