001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.log;
022:
023: import com.liferay.portal.kernel.util.StackTraceUtil;
024:
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Properties;
031:
032: import javax.servlet.ServletException;
033: import javax.servlet.jsp.JspException;
034:
035: /**
036: * <a href="LogUtil.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class LogUtil {
042:
043: public static final int STACK_TRACE_LENGTH = 20;
044:
045: public static final boolean REMOVE_UNKNOWN_SOURCE = true;
046:
047: public static void debug(Log log, Properties props) {
048: if (log.isDebugEnabled()) {
049: StringWriter sw = new StringWriter();
050:
051: props.list(new PrintWriter(sw));
052:
053: log.debug(sw.getBuffer().toString());
054: }
055: }
056:
057: public static void log(Log log, Throwable t) {
058: if (t instanceof JspException) {
059: log(log, (JspException) t);
060: } else if (t instanceof ServletException) {
061: log(log, (ServletException) t);
062: } else {
063: Throwable cause = t.getCause();
064:
065: if (cause != null) {
066: log(log, cause);
067: } else {
068: _log(log, t);
069: }
070: }
071: }
072:
073: public static void log(Log log, JspException jspe) {
074: Throwable cause = jspe.getRootCause();
075:
076: if (cause == null) {
077: cause = jspe;
078: }
079:
080: if ((cause != jspe) && (cause instanceof JspException)) {
081: log(log, (JspException) cause);
082: } else if (cause instanceof ServletException) {
083: log(log, (ServletException) cause);
084: } else {
085: _log(log, cause);
086: }
087: }
088:
089: public static void log(Log log, ServletException se) {
090: Throwable cause = se.getRootCause();
091:
092: if (cause == null) {
093: cause = se;
094: }
095:
096: if (cause instanceof JspException) {
097: log(log, (JspException) cause);
098: } else if ((cause != se) && (cause instanceof ServletException)) {
099: log(log, (ServletException) cause);
100: } else {
101: _log(log, cause);
102: }
103: }
104:
105: private static void _log(Log log, Throwable cause) {
106: StackTraceElement[] steArray = (StackTraceElement[]) cause
107: .getStackTrace();
108:
109: // Make the stack trace more readable by limiting the number of
110: // elements.
111:
112: if (steArray.length > STACK_TRACE_LENGTH) {
113: int count = 0;
114:
115: List steList = new ArrayList();
116:
117: for (int i = 0; i < steArray.length; i++) {
118: StackTraceElement ste = (StackTraceElement) steArray[i];
119:
120: // Make the stack trace more readable by removing elements that
121: // refer to classes with no packages, or starts with a $, or are
122: // Spring classes, or are standard reflection classes.
123:
124: String className = ste.getClassName();
125:
126: boolean addElement = true;
127:
128: if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
129: addElement = false;
130: }
131:
132: if (className.startsWith("$")
133: || className.startsWith("java.lang.reflect.")
134: || className.startsWith("org.springframework.")
135: || className.startsWith("sun.reflect.")) {
136:
137: addElement = false;
138: }
139:
140: if (addElement) {
141: steList.add(ste);
142:
143: count++;
144: }
145:
146: if (count >= STACK_TRACE_LENGTH) {
147: break;
148: }
149: }
150:
151: steArray = (StackTraceElement[]) steList
152: .toArray(new StackTraceElement[0]);
153:
154: cause.setStackTrace(steArray);
155: }
156:
157: log.error(StackTraceUtil.getStackTrace(cause));
158: }
159:
160: }
|