001
014
015 package com.liferay.portal.kernel.log;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
019 import com.liferay.portal.kernel.util.StackTraceUtil;
020
021 import java.util.ArrayList;
022 import java.util.List;
023 import java.util.Properties;
024
025 import javax.servlet.ServletException;
026 import javax.servlet.jsp.JspException;
027
028
031 public class LogUtil {
032
033 public static final int STACK_TRACE_LENGTH = 20;
034
035 public static final boolean REMOVE_UNKNOWN_SOURCE = true;
036
037 public static void debug(Log log, Properties props) {
038 if (log.isDebugEnabled()) {
039 UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(
040 props.size() + 1);
041
042 props.list(new UnsyncPrintWriter(unsyncStringWriter));
043
044 log.debug(unsyncStringWriter.toString());
045 }
046 }
047
048 public static void log(Log log, Throwable t) {
049 if (t instanceof JspException) {
050 log(log, (JspException)t);
051 }
052 else if (t instanceof ServletException) {
053 log(log, (ServletException)t);
054 }
055 else {
056 Throwable cause = t.getCause();
057
058 if (cause != null) {
059 log(log, cause);
060 }
061 else {
062 _log(log, t);
063 }
064 }
065 }
066
067 public static void log(Log log, JspException jspe) {
068 Throwable cause = jspe.getCause();
069
070 if (cause == null) {
071 cause = jspe;
072 }
073
074 if ((cause != jspe) && (cause instanceof JspException)) {
075 log(log, (JspException)cause);
076 }
077 else if (cause instanceof ServletException) {
078 log(log, (ServletException)cause);
079 }
080 else {
081 _log(log, cause);
082 }
083 }
084
085 public static void log(Log log, ServletException se) {
086 Throwable cause = se.getRootCause();
087
088 if (cause == null) {
089 cause = se;
090 }
091
092 if (cause instanceof JspException) {
093 log(log, (JspException)cause);
094 }
095 else if ((cause != se) && (cause instanceof ServletException)) {
096 log(log, (ServletException)cause);
097 }
098 else {
099 _log(log, cause);
100 }
101 }
102
103 private static void _log(Log log, Throwable cause) {
104 StackTraceElement[] steArray = cause.getStackTrace();
105
106
107
108
109 if (steArray.length > STACK_TRACE_LENGTH) {
110 int count = 0;
111
112 List<StackTraceElement> steList =
113 new ArrayList<StackTraceElement>();
114
115 for (int i = 0; i < steArray.length; i++) {
116 StackTraceElement ste = steArray[i];
117
118
119
120
121
122 String className = ste.getClassName();
123
124 boolean addElement = true;
125
126 if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
127 addElement = false;
128 }
129
130 if (className.startsWith("$") ||
131 className.startsWith("java.lang.reflect.") ||
132 className.startsWith("org.springframework.") ||
133 className.startsWith("sun.reflect.")) {
134
135 addElement = false;
136 }
137
138 if (addElement) {
139 steList.add(ste);
140
141 count++;
142 }
143
144 if (count >= STACK_TRACE_LENGTH) {
145 break;
146 }
147 }
148
149 steArray = steList.toArray(new StackTraceElement[steList.size()]);
150
151 cause.setStackTrace(steArray);
152 }
153
154 log.error(StackTraceUtil.getStackTrace(cause));
155 }
156
157 }