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