1
2
3
4
5
6
7
8
9
10
11
12
13
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
52
53 public SimpleExceptionLoggingAdvice() {
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public void setLoggingLevel(String loggingLevel) {
70 this.loggingLevel = LoggingLevel.valueOf(loggingLevel.toUpperCase());
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public void setExceptionLoggingType(String exceptionType) {
87 this.exceptionType = ExceptionLevel.valueOf(exceptionType.toUpperCase());
88 }
89
90
91
92
93
94
95
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
117
118
119
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 }