001
014
015 package com.liferay.portal.kernel.log;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018 import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
019 import com.liferay.portal.kernel.util.Validator;
020
021 import java.util.ArrayList;
022 import java.util.List;
023 import java.util.Properties;
024
025
028 public class LogUtil {
029
030 public static final boolean REMOVE_UNKNOWN_SOURCE = true;
031
032 public static final int STACK_TRACE_LENGTH = 20;
033
034 public static void debug(Log log, Properties props) {
035 if (log.isDebugEnabled()) {
036 UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(
037 props.size() + 1);
038
039 props.list(UnsyncPrintWriterPool.borrow(unsyncStringWriter));
040
041 log.debug(unsyncStringWriter.toString());
042 }
043 }
044
045 public static void log(Log log, Throwable throwable) {
046 log(log, throwable, null);
047 }
048
049 public static void log(Log log, Throwable throwable, String message) {
050 if (throwable == null) {
051 if (Validator.isNotNull(message)) {
052 log.error(message);
053
054 return;
055 }
056
057 throw new IllegalArgumentException(
058 "Throwable or message must be set");
059 }
060
061 Throwable causeThrowable = throwable;
062
063 while (causeThrowable.getCause() != null) {
064 causeThrowable = causeThrowable.getCause();
065 }
066
067 StackTraceElement[] stackTraceElements = causeThrowable.getStackTrace();
068
069
070
071
072 if (stackTraceElements.length <= STACK_TRACE_LENGTH) {
073 if (Validator.isNotNull(message)) {
074 log.error(message, causeThrowable);
075 }
076 else {
077 log.error(causeThrowable);
078 }
079
080 return;
081 }
082
083 int count = 0;
084
085 List<StackTraceElement> stackTraceElementsList =
086 new ArrayList<StackTraceElement>();
087
088 for (StackTraceElement stackTraceElement : stackTraceElements) {
089
090
091
092
093
094 String className = stackTraceElement.getClassName();
095
096 boolean addElement = true;
097
098 if (REMOVE_UNKNOWN_SOURCE &&
099 (stackTraceElement.getLineNumber() < 0)) {
100
101 addElement = false;
102 }
103
104 if (className.startsWith("$") ||
105 className.startsWith("java.lang.reflect.") ||
106 className.startsWith("org.springframework.") ||
107 className.startsWith("sun.reflect.")) {
108
109 addElement = false;
110 }
111
112 if (addElement) {
113 stackTraceElementsList.add(stackTraceElement);
114
115 count++;
116 }
117
118 if (count >= STACK_TRACE_LENGTH) {
119 break;
120 }
121 }
122
123 stackTraceElements = stackTraceElementsList.toArray(
124 new StackTraceElement[stackTraceElementsList.size()]);
125
126 causeThrowable.setStackTrace(stackTraceElements);
127
128 if (Validator.isNotNull(message)) {
129 log.error(message, causeThrowable);
130 }
131 else {
132 log.error(causeThrowable);
133 }
134 }
135
136 }