Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleExceptionLoggingAdvice |
|
| 3.6;3.6 | ||||
SimpleExceptionLoggingAdvice$1 |
|
| 3.6;3.6 | ||||
SimpleExceptionLoggingAdvice$ExceptionLevel |
|
| 3.6;3.6 | ||||
SimpleExceptionLoggingAdvice$LoggingLevel |
|
| 3.6;3.6 |
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 | * <aop:config> | |
30 | * <aop:aspect id="exceptionLoggingAspect" ref="exceptionLoggingAdvice" order="2"> | |
31 | * <aop:after-throwing | |
32 | * pointcut="execution(* org.kuali.student.lum.lu.service.*.*(..))" | |
33 | * method="afterThrowing" throwing="t" /> | |
34 | * </aop:aspect> | |
35 | * </aop:config> | |
36 | * <bean id="exceptionLoggingAdvice" class="org.kuali.student.common.util.SimpleExceptionLoggingAdvice"> | |
37 | * <property name="loggingLevel" value="INFO" /> | |
38 | * <property name="exceptionLoggingType" value="RUNTIME" /> | |
39 | * </bean> | |
40 | * </pre> | |
41 | */ | |
42 | public class SimpleExceptionLoggingAdvice implements ThrowsAdvice { | |
43 | ||
44 | 0 | private enum ExceptionLevel{THROWABLE, EXCEPTION, RUNTIME}; |
45 | 0 | private ExceptionLevel exceptionType = ExceptionLevel.THROWABLE; |
46 | ||
47 | 0 | private enum LoggingLevel{NONE, DEBUG, INFO, WARN, ERROR, STACKTRACE}; |
48 | 0 | private LoggingLevel loggingLevel = LoggingLevel.STACKTRACE; |
49 | ||
50 | /** | |
51 | * Constructor. | |
52 | */ | |
53 | 0 | public SimpleExceptionLoggingAdvice() { |
54 | 0 | } |
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 | 0 | this.loggingLevel = LoggingLevel.valueOf(loggingLevel.toUpperCase()); |
71 | 0 | } |
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 | 0 | this.exceptionType = ExceptionLevel.valueOf(exceptionType.toUpperCase()); |
88 | 0 | } |
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 | 0 | switch(this.exceptionType) { |
99 | case THROWABLE: | |
100 | 0 | if(t instanceof Throwable) { |
101 | 0 | logException(jp.getTarget().getClass(), t); |
102 | } | |
103 | case EXCEPTION: | |
104 | 0 | if(t instanceof Exception) { |
105 | 0 | logException(jp.getTarget().getClass(), t); |
106 | } | |
107 | 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 | private void logException(Class<?> targetClass, Throwable t) { | |
122 | 0 | Logger logger = LoggerFactory.getLogger(targetClass); |
123 | ||
124 | 0 | switch(this.loggingLevel) { |
125 | case NONE: | |
126 | 0 | break; |
127 | case DEBUG: | |
128 | 0 | logger.debug(t.getMessage(), t); |
129 | 0 | break; |
130 | case INFO: | |
131 | 0 | logger.info(t.getMessage(), t); |
132 | 0 | break; |
133 | case WARN: | |
134 | 0 | logger.warn(t.getMessage(), t); |
135 | 0 | break; |
136 | case ERROR: | |
137 | 0 | logger.error(t.getMessage(), t); |
138 | 0 | break; |
139 | case STACKTRACE: | |
140 | 0 | logger.error(t.getMessage(), t); |
141 | break; | |
142 | } | |
143 | 0 | } |
144 | } |