View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.util;
17  
18  import org.aspectj.lang.JoinPoint;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.aop.ThrowsAdvice;
22  
23  /**
24   * Advice to log exceptions. 
25   * Default <code>loggingLevel=STACKTRACE</code> and <code>exceptionLoggingType=THROWABLE</code>.
26   * 
27   * <p>Spring configuration example</p>
28   * <pre>
29   * &lt;aop:config&gt;
30   *     &lt;aop:aspect id="exceptionLoggingAspect" ref="exceptionLoggingAdvice" order="2"&gt;
31   *         &lt;aop:after-throwing
32   *             pointcut="execution(* org.kuali.student.lum.lu.service.*.*(..))"
33   *             method="afterThrowing" throwing="t" /&gt;
34   *     &lt;/aop:aspect&gt;
35   * &lt;/aop:config&gt;
36   * &lt;bean id="exceptionLoggingAdvice" class="org.kuali.student.common.util.SimpleExceptionLoggingAdvice"&gt;
37   *     &lt;property name="loggingLevel" value="INFO" /&gt;
38   *     &lt;property name="exceptionLoggingType" value="RUNTIME" /&gt;
39   * &lt;/bean&gt;
40   * </pre>
41   */
42  public class SimpleExceptionLoggingAdvice implements ThrowsAdvice {
43  
44  	private enum ExceptionLevel{THROWABLE, EXCEPTION, RUNTIME};
45  	private ExceptionLevel exceptionType = ExceptionLevel.THROWABLE;
46  
47  	private enum LoggingLevel{NONE, DEBUG, INFO, WARN, ERROR, STACKTRACE};
48  	private LoggingLevel loggingLevel = LoggingLevel.STACKTRACE;
49  	
50  	/**
51  	 * Constructor.
52  	 */
53  	public SimpleExceptionLoggingAdvice() {
54  	}
55  	
56  	/**
57  	 * Sets the logging level.
58  	 * <p>Logging levels:</p>
59  	 * <ul>
60  	 * <li>NONE</li>
61  	 * <li>DEBUG</li>
62  	 * <li>INFO</li>
63  	 * <li>WARN</li>
64  	 * <li>ERROR</li>
65  	 * <li>STACKTRACE</li>
66  	 * </ul>
67  	 * @param loggingLevel Logging level
68  	 */
69  	public void setLoggingLevel(String loggingLevel) {
70  		this.loggingLevel = LoggingLevel.valueOf(loggingLevel.toUpperCase());
71  	}
72  
73  	/**
74  	 * Sets the type of exception to log. 
75  	 * E.g. Only log runtime exception (<code>RUNTIME</code>).
76  	 * 
77  	 * <p>Exception types:</p>
78  	 * <ul>
79  	 * <li>THROWABLE</li>
80  	 * <li>EXCEPTION</li>
81  	 * <li>RUNTIME</li>
82  	 * </ul>
83  	 *  
84  	 * @param exceptionType exception logging type
85  	 */
86  	public void setExceptionLoggingType(String exceptionType) {
87  		this.exceptionType = ExceptionLevel.valueOf(exceptionType.toUpperCase());
88  	}
89  
90  	/**
91  	 * Catches the exception being thrown.
92  	 * 
93  	 * @param jp Aspect join point
94  	 * @param t Exception being thrown
95  	 * @throws Throwable
96  	 */
97      public void afterThrowing(JoinPoint jp, Throwable t) throws Throwable {
98      	switch(this.exceptionType) {
99  			case THROWABLE:
100 		    	if(t instanceof Throwable) {
101 		    		logException(jp.getTarget().getClass(), t);
102 		    	}		    		
103 			case EXCEPTION:
104 		    	if(t instanceof Exception) {
105 		    		logException(jp.getTarget().getClass(), t);
106 		    	}		    		
107 			case RUNTIME:
108 		    	if(t instanceof RuntimeException) {
109 		    		logException(jp.getTarget().getClass(), t);
110 		    	}		    		
111 		}
112 		throw t;
113 	}
114     
115     /**
116      * Logs the exception.
117      * 
118      * @param targetClass The join point class the exception was caught.
119      * @param t Exception being thrown.
120      */
121     private void logException(Class<?> targetClass, Throwable t) {
122     	Logger logger = LoggerFactory.getLogger(targetClass);
123 
124     	switch(this.loggingLevel) {
125 			case NONE:
126 				break;
127 			case DEBUG:
128 		    	logger.debug(t.getMessage(), t);
129 				break;
130 			case INFO:
131 		    	logger.info(t.getMessage(), t);
132 				break;
133 			case WARN:
134 		    	logger.warn(t.getMessage(), t);
135 				break;
136 			case ERROR:
137 		    	logger.error(t.getMessage(), t);
138 				break;
139 			case STACKTRACE:
140 		    	logger.error(t.getMessage(), t);
141 				break;
142 		}
143     }
144 }