001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
029     * @author Brian Wing Shun Chan
030     */
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                    // Make the stack trace more readable by limiting the number of
107                    // elements.
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                                    // Make the stack trace more readable by removing elements that
119                                    // refer to classes with no packages, or starts with a $, or are
120                                    // Spring classes, or are standard reflection classes.
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    }