2
2
3
3
import com .exceptionless .exceptionlessclient .configuration .Configuration ;
4
4
import com .exceptionless .exceptionlessclient .configuration .ConfigurationManager ;
5
+ import com .exceptionless .exceptionlessclient .enums .EventPropertyKey ;
6
+ import com .exceptionless .exceptionlessclient .enums .EventType ;
5
7
import com .exceptionless .exceptionlessclient .models .Event ;
6
8
import com .exceptionless .exceptionlessclient .models .EventPluginContext ;
7
9
import com .exceptionless .exceptionlessclient .models .PluginContext ;
8
10
import com .exceptionless .exceptionlessclient .models .UserDescription ;
9
- import com .exceptionless .exceptionlessclient .models .enums .EventPropertyKey ;
10
- import com .exceptionless .exceptionlessclient .models .enums .EventType ;
11
- import com .exceptionless .exceptionlessclient .models .submission .SubmissionResponse ;
12
11
import com .exceptionless .exceptionlessclient .plugins .EventPluginRunner ;
12
+ import com .exceptionless .exceptionlessclient .submission .SubmissionResponse ;
13
13
import com .exceptionless .exceptionlessclient .utils .VisibleForTesting ;
14
14
import lombok .Builder ;
15
15
import lombok .Getter ;
16
- import org .slf4j .Logger ;
17
- import org .slf4j .LoggerFactory ;
16
+ import lombok .extern .slf4j .Slf4j ;
18
17
19
18
import java .time .LocalDate ;
20
19
import java .util .Timer ;
23
22
import java .util .concurrent .ExecutorService ;
24
23
import java .util .concurrent .Executors ;
25
24
25
+ @ Slf4j
26
26
public class ExceptionlessClient {
27
- private static final Logger LOG = LoggerFactory .getLogger (ExceptionlessClient .class );
28
27
private static final String UPDATE_SETTINGS_TIMER_NAME = "update-settings-timer" ;
29
28
private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000 ;
30
29
private static final Integer DEFAULT_NTHREADS = 10 ;
@@ -63,7 +62,7 @@ public void run() {
63
62
try {
64
63
configurationManager .getSettingsManager ().updateSettings ();
65
64
} catch (Exception e ) {
66
- LOG .error ("Error in updating settings" , e );
65
+ log .error ("Error in updating settings" , e );
67
66
}
68
67
}
69
68
},
@@ -92,12 +91,26 @@ public CompletableFuture<Void> submitExceptionAsync(Exception exception) {
92
91
}
93
92
94
93
public void submitException (Exception exception ) {
95
- Event event = createException ().build ();
94
+ submitException (null , exception );
95
+ }
96
+
97
+ public CompletableFuture <Void > submitExceptionAsync (String message , Exception exception ) {
98
+ return CompletableFuture .runAsync (() -> submitException (message , exception ), executorService );
99
+ }
100
+
101
+ public void submitException (String message , Exception exception ) {
102
+ Event event ;
103
+ if (message == null ) {
104
+ event = createError ().build ();
105
+ } else {
106
+ event = createError ().message (message ).build ();
107
+ }
96
108
PluginContext pluginContext = PluginContext .builder ().exception (exception ).build ();
97
- submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
109
+ submitEventWithContext (
110
+ EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
98
111
}
99
112
100
- public Event .EventBuilder createException () {
113
+ public Event .EventBuilder createError () {
101
114
return createEvent ().type (EventType .ERROR .value ());
102
115
}
103
116
@@ -108,23 +121,23 @@ public CompletableFuture<Void> submitUnhandledExceptionAsync(
108
121
}
109
122
110
123
public void submitUnhandledException (Exception exception , String submissionMethod ) {
111
- Event event = createException ().build ();
124
+ Event event = createError ().build ();
112
125
PluginContext pluginContext =
113
126
PluginContext .builder ()
114
127
.exception (exception )
115
128
.unhandledError (true )
116
129
.submissionMethod (submissionMethod )
117
130
.build ();
118
- submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
131
+ submitEventWithContext (
132
+ EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
119
133
}
120
134
121
135
public CompletableFuture <Void > submitFeatureUsageAsync (String feature ) {
122
136
return CompletableFuture .runAsync (() -> submitFeatureUsage (feature ), executorService );
123
137
}
124
138
125
139
public void submitFeatureUsage (String feature ) {
126
- Event event = createFeatureUsage (feature ).build ();
127
- submitEvent (EventPluginContext .from (event ));
140
+ submitEvent (createFeatureUsage (feature ).build ());
128
141
}
129
142
130
143
public Event .EventBuilder createFeatureUsage (String feature ) {
@@ -152,8 +165,7 @@ public CompletableFuture<Void> submitLogAsync(String message, String source, Str
152
165
}
153
166
154
167
public void submitLog (String message , String source , String level ) {
155
- Event event = createLog (message , source , level ).build ();
156
- submitEvent (EventPluginContext .from (event ));
168
+ submitEvent (createLog (message , source , level ).build ());
157
169
}
158
170
159
171
public Event .EventBuilder createLog (String message ) {
@@ -166,13 +178,7 @@ public Event.EventBuilder createLog(String message, String source) {
166
178
167
179
public Event .EventBuilder createLog (String message , String source , String level ) {
168
180
if (source == null ) {
169
- // Calling method
170
- StackTraceElement [] traceElements = Thread .currentThread ().getStackTrace ();
171
- source = traceElements [2 ].getMethodName ();
172
- // Came from the overrided method
173
- if (source .equals ("createLog" )) {
174
- source = traceElements [3 ].getMethodName ();
175
- }
181
+ source = getCallingMethod ();
176
182
}
177
183
178
184
Event .EventBuilder builder =
@@ -184,13 +190,20 @@ public Event.EventBuilder createLog(String message, String source, String level)
184
190
return builder .property (EventPropertyKey .LOG_LEVEL .value (), level );
185
191
}
186
192
193
+ private String getCallingMethod () {
194
+ StackTraceElement [] traceElements = Thread .currentThread ().getStackTrace ();
195
+ String source = traceElements [3 ].getMethodName ();
196
+ boolean cameFromOverridenMethod = source .equals ("createLog" );
197
+
198
+ return cameFromOverridenMethod ? traceElements [4 ].getMethodName () : source ;
199
+ }
200
+
187
201
public CompletableFuture <Void > submitNotFoundAsync (String resource ) {
188
202
return CompletableFuture .runAsync (() -> submitNotFound (resource ), executorService );
189
203
}
190
204
191
205
public void submitNotFound (String resource ) {
192
- Event event = createNotFound (resource ).build ();
193
- submitEvent (EventPluginContext .from (event ));
206
+ submitEvent (createNotFound (resource ).build ());
194
207
}
195
208
196
209
public Event .EventBuilder createNotFound (String resource ) {
@@ -202,8 +215,7 @@ public CompletableFuture<Void> submitSessionStartAsync() {
202
215
}
203
216
204
217
public void submitSessionStart () {
205
- Event event = createSessionStart ().build ();
206
- submitEvent (EventPluginContext .from (event ));
218
+ submitEvent (createSessionStart ().build ());
207
219
}
208
220
209
221
public Event .EventBuilder createSessionStart () {
@@ -216,11 +228,21 @@ public Event.EventBuilder createEvent() {
216
228
.date (LocalDate .now ());
217
229
}
218
230
219
- public CompletableFuture <Void > submitEventAsync (EventPluginContext eventPluginContext ) {
220
- return CompletableFuture .runAsync (() -> submitEvent (eventPluginContext ), executorService );
231
+ public CompletableFuture <Void > submitEventAsync (Event event ) {
232
+ return CompletableFuture .runAsync (() -> submitEvent (event ), executorService );
233
+ }
234
+
235
+ public void submitEvent (Event event ) {
236
+ eventPluginRunner .run (EventPluginContext .from (event ));
221
237
}
222
238
223
- public void submitEvent (EventPluginContext eventPluginContext ) {
239
+ public CompletableFuture <Void > submitEventWithContextAsync (
240
+ EventPluginContext eventPluginContext ) {
241
+ return CompletableFuture .runAsync (
242
+ () -> submitEventWithContext (eventPluginContext ), executorService );
243
+ }
244
+
245
+ public void submitEventWithContext (EventPluginContext eventPluginContext ) {
224
246
eventPluginRunner .run (eventPluginContext );
225
247
}
226
248
@@ -229,7 +251,7 @@ public CompletableFuture<Void> submitSessionEndAsync(String sessionOrUserId) {
229
251
}
230
252
231
253
public void submitSessionEnd (String sessionOrUserId ) {
232
- LOG .info (String .format ("Submitting session end: %s" , sessionOrUserId ));
254
+ log .info (String .format ("Submitting session end: %s" , sessionOrUserId ));
233
255
configurationManager .getSubmissionClient ().sendHeartBeat (sessionOrUserId , true );
234
256
}
235
257
@@ -247,9 +269,16 @@ public SubmissionResponse updateEmailAndDescription(
247
269
.postUserDescription (
248
270
referenceId ,
249
271
UserDescription .builder ().description (description ).emailAddress (email ).build ());
272
+ if (response .hasException ()) {
273
+ log .error (
274
+ String .format ("Failed to submit user email and description for event: %s" , referenceId ),
275
+ response .getException ());
276
+ }
250
277
if (!response .isSuccess ()) {
251
- LOG .error (
252
- String .format ("Failed to submit user email and description for event: %s" , referenceId ));
278
+ log .error (
279
+ String .format (
280
+ "Failed to submit user email and description for event: %s, code: %s" ,
281
+ referenceId , response .getCode ()));
253
282
}
254
283
255
284
return response ;
0 commit comments