Clover Coverage Report - KS Common Util 1.1-SNAPSHOT
Coverage timestamp: Thu Mar 3 2011 04:41:37 EST
../../../../../img/srcFileCovDistChart0.png 41% of files have more coverage
32   144   17   6.4
6   58   0.53   1.67
5     3.4  
3    
 
  SimpleExceptionLoggingAdvice       Line # 42 32 0% 17 43 0% 0.0
  SimpleExceptionLoggingAdvice.ExceptionLevel       Line # 44 0 - 0 0 - -1.0
  SimpleExceptionLoggingAdvice.LoggingLevel       Line # 47 0 - 0 0 - -1.0
 
No Tests
 
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  0 toggle 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  0 toggle public void setLoggingLevel(String loggingLevel) {
70  0 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  0 toggle public void setExceptionLoggingType(String exceptionType) {
87  0 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  0 toggle public void afterThrowing(JoinPoint jp, Throwable t) throws Throwable {
98  0 switch(this.exceptionType) {
99  0 case THROWABLE:
100  0 if(t instanceof Throwable) {
101  0 logException(jp.getTarget().getClass(), t);
102    }
103  0 case EXCEPTION:
104  0 if(t instanceof Exception) {
105  0 logException(jp.getTarget().getClass(), t);
106    }
107  0 case RUNTIME:
108  0 if(t instanceof RuntimeException) {
109  0 logException(jp.getTarget().getClass(), t);
110    }
111    }
112  0 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  0 toggle private void logException(Class<?> targetClass, Throwable t) {
122  0 Logger logger = LoggerFactory.getLogger(targetClass);
123   
124  0 switch(this.loggingLevel) {
125  0 case NONE:
126  0 break;
127  0 case DEBUG:
128  0 logger.debug(t.getMessage(), t);
129  0 break;
130  0 case INFO:
131  0 logger.info(t.getMessage(), t);
132  0 break;
133  0 case WARN:
134  0 logger.warn(t.getMessage(), t);
135  0 break;
136  0 case ERROR:
137  0 logger.error(t.getMessage(), t);
138  0 break;
139  0 case STACKTRACE:
140  0 logger.error(t.getMessage(), t);
141  0 break;
142    }
143    }
144    }