1 package com.github.sfleiter.cdi_interceptors;
2
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Method;
5
6 import javax.inject.Inject;
7 import javax.interceptor.AroundInvoke;
8 import javax.interceptor.Interceptor;
9 import javax.interceptor.InvocationContext;
10
11 import uk.org.lidalia.slf4jext.Level;
12 import uk.org.lidalia.slf4jext.Logger;
13 import uk.org.lidalia.slf4jext.LoggerFactory;
14
15 import com.github.sfleiter.cdi_interceptors.impl.StringTransformer;
16 import com.thoughtworks.paranamer.AdaptiveParanamer;
17 import com.thoughtworks.paranamer.BytecodeReadingParanamer;
18 import com.thoughtworks.paranamer.CachingParanamer;
19 import com.thoughtworks.paranamer.Paranamer;
20 import com.thoughtworks.paranamer.PositionalParanamer;
21
22
23
24
25
26
27
28
29
30
31
32 @Interceptor
33 @Logging
34 public class LoggingInterceptor {
35
36
37 private final Paranamer paranamer = new CachingParanamer(
38 new AdaptiveParanamer(
39 new BytecodeReadingParanamer(),
40 new PositionalParanamer()
41 ));
42
43 @Inject
44 private StringTransformer stringTransformer;
45
46
47
48
49
50
51
52
53
54
55 @AroundInvoke
56 public Object loggingMethodInvoke(final InvocationContext ctx) throws Exception {
57 Logger logger = LoggerFactory.getLogger(ctx.getTarget().getClass()
58 .getName());
59 Logging annotation = getAnnotation(ctx.getMethod(), Logging.class);
60 Level level = annotation.standardLogLevel();
61 int maximumCount = annotation.logItemLimit();
62 boolean measureDuration = annotation.measureDuration();
63
64 StringBuilder sb;
65 long start = currentTimeMillis(measureDuration);
66 long duration;
67 Object result;
68 try {
69 result = ctx.proceed();
70 if (logger.isEnabled(level)) {
71 duration = currentTimeMillis(measureDuration) - start;
72 sb = getCallString(ctx, maximumCount);
73 sb.append(" returns ");
74 stringTransformer.transform(sb, result, maximumCount);
75 appendDuration(sb, measureDuration, duration);
76 logger.log(level, sb.toString());
77 }
78 } catch (Exception e) {
79 if (e instanceof RuntimeException) {
80 level = annotation.severeExceptionLogLevel();
81 }
82 if (logger.isEnabled(level)) {
83 duration = currentTimeMillis(measureDuration) - start;
84 sb = getCallString(ctx, maximumCount);
85 sb.append(" caused ");
86 sb.append(e.getMessage());
87 appendDuration(sb, measureDuration, duration);
88 logger.log(level, sb.toString(), e);
89 }
90 throw e;
91 }
92 return result;
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 private <T extends Annotation> T getAnnotation(final Method method, final Class<T> annotationClazz)
111 throws RuntimeException {
112
113 for (Annotation a : method.getAnnotations()) {
114 if (annotationClazz.isInstance(a)) {
115 return annotationClazz.cast(a);
116 }
117 }
118 for (Annotation a : method.getDeclaringClass().getAnnotations()) {
119 if (annotationClazz.isInstance(a)) {
120 return annotationClazz.cast(a);
121 }
122 }
123 throw new RuntimeException("@" + annotationClazz.getName() + " not found on method " + method.getName()
124 + " or its class " + method.getClass().getName());
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138 private StringBuilder getCallString(final InvocationContext ctx, final int maximumCount) {
139 StringBuilder sb = new StringBuilder();
140 sb.append("call ");
141 sb.append(ctx.getMethod().getName());
142 sb.append("(");
143 Object[] parameters = ctx.getParameters();
144 String[] parameterNames = paranamer.lookupParameterNames(
145 ctx.getMethod(), false);
146 for (int i = 0; i < parameters.length; i++) {
147 sb.append(parameterNames[i]);
148 sb.append("=");
149 stringTransformer.transform(sb, parameters[i], maximumCount);
150 if (i < parameterNames.length - 1) {
151 sb.append(", ");
152 }
153 }
154 sb.append(")");
155 return sb;
156 }
157
158
159
160
161
162
163
164
165 private long currentTimeMillis(final boolean measureDuration) {
166 return measureDuration ? System.currentTimeMillis() : 0;
167 }
168
169
170
171
172
173
174
175
176
177
178
179 private void appendDuration(final StringBuilder sb, final boolean measureDuration, final long duration) {
180 if (measureDuration) {
181 sb.append(" [duration=");
182 sb.append(duration);
183 sb.append("ms]");
184 }
185 }
186
187 }