001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.StackTraceUtil;
019    import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
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 boolean REMOVE_UNKNOWN_SOURCE = true;
034    
035            public static final int STACK_TRACE_LENGTH = 20;
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(UnsyncPrintWriterPool.borrow(unsyncStringWriter));
043    
044                            log.debug(unsyncStringWriter.toString());
045                    }
046            }
047    
048            public static void log(Log log, JspException jspe) {
049                    Throwable cause = jspe.getCause();
050    
051                    if (cause == null) {
052                            cause = jspe;
053                    }
054    
055                    if ((cause != jspe) && (cause instanceof JspException)) {
056                            log(log, (JspException)cause);
057                    }
058                    else if (cause instanceof ServletException) {
059                            log(log, (ServletException)cause);
060                    }
061                    else {
062                            _log(log, cause);
063                    }
064            }
065    
066            public static void log(Log log, ServletException se) {
067                    Throwable cause = se.getRootCause();
068    
069                    if (cause == null) {
070                            cause = se;
071                    }
072    
073                    if (cause instanceof JspException) {
074                            log(log, (JspException)cause);
075                    }
076                    else if ((cause != se) && (cause instanceof ServletException)) {
077                            log(log, (ServletException)cause);
078                    }
079                    else {
080                            _log(log, cause);
081                    }
082            }
083    
084            public static void log(Log log, Throwable t) {
085                    if (t instanceof JspException) {
086                            log(log, (JspException)t);
087                    }
088                    else if (t instanceof ServletException) {
089                            log(log, (ServletException)t);
090                    }
091                    else {
092                            Throwable cause = t.getCause();
093    
094                            if (cause != null) {
095                                    log(log, cause);
096                            }
097                            else {
098                                    _log(log, t);
099                            }
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                            log.error(StackTraceUtil.getStackTrace(cause));
111    
112                            return;
113                    }
114    
115                    int count = 0;
116    
117                    List<StackTraceElement> steList = new ArrayList<StackTraceElement>();
118    
119                    for (int i = 0; i < steArray.length; i++) {
120                            StackTraceElement ste = steArray[i];
121    
122                            // Make the stack trace more readable by removing elements that
123                            // refer to classes with no packages, or starts with a $, or are
124                            // Spring classes, or are standard reflection classes.
125    
126                            String className = ste.getClassName();
127    
128                            boolean addElement = true;
129    
130                            if (REMOVE_UNKNOWN_SOURCE && (ste.getLineNumber() < 0)) {
131                                    addElement = false;
132                            }
133    
134                            if (className.startsWith("$") ||
135                                    className.startsWith("java.lang.reflect.") ||
136                                    className.startsWith("org.springframework.") ||
137                                    className.startsWith("sun.reflect.")) {
138    
139                                    addElement = false;
140                            }
141    
142                            if (addElement) {
143                                    steList.add(ste);
144    
145                                    count++;
146                            }
147    
148                            if (count >= STACK_TRACE_LENGTH) {
149                                    break;
150                            }
151                    }
152    
153                    steArray = steList.toArray(new StackTraceElement[steList.size()]);
154    
155                    cause.setStackTrace(steArray);
156    
157                    log.error(StackTraceUtil.getStackTrace(cause));
158            }
159    
160    }