22
33import com .exceptionless .exceptionlessclient .configuration .Configuration ;
44import com .exceptionless .exceptionlessclient .configuration .ConfigurationManager ;
5+ import com .exceptionless .exceptionlessclient .enums .EventPropertyKey ;
6+ import com .exceptionless .exceptionlessclient .enums .EventType ;
57import com .exceptionless .exceptionlessclient .models .Event ;
68import com .exceptionless .exceptionlessclient .models .EventPluginContext ;
79import com .exceptionless .exceptionlessclient .models .PluginContext ;
810import 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 ;
1211import com .exceptionless .exceptionlessclient .plugins .EventPluginRunner ;
12+ import com .exceptionless .exceptionlessclient .submission .SubmissionResponse ;
1313import com .exceptionless .exceptionlessclient .utils .VisibleForTesting ;
1414import lombok .Builder ;
1515import lombok .Getter ;
16- import org .slf4j .Logger ;
17- import org .slf4j .LoggerFactory ;
16+ import lombok .extern .slf4j .Slf4j ;
1817
1918import java .time .LocalDate ;
2019import java .util .Timer ;
2322import java .util .concurrent .ExecutorService ;
2423import java .util .concurrent .Executors ;
2524
25+ @ Slf4j
2626public class ExceptionlessClient {
27- private static final Logger LOG = LoggerFactory .getLogger (ExceptionlessClient .class );
2827 private static final String UPDATE_SETTINGS_TIMER_NAME = "update-settings-timer" ;
2928 private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000 ;
3029 private static final Integer DEFAULT_NTHREADS = 10 ;
@@ -63,7 +62,7 @@ public void run() {
6362 try {
6463 configurationManager .getSettingsManager ().updateSettings ();
6564 } catch (Exception e ) {
66- LOG .error ("Error in updating settings" , e );
65+ log .error ("Error in updating settings" , e );
6766 }
6867 }
6968 },
@@ -92,12 +91,26 @@ public CompletableFuture<Void> submitExceptionAsync(Exception exception) {
9291 }
9392
9493 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+ }
96108 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 ());
98111 }
99112
100- public Event .EventBuilder createException () {
113+ public Event .EventBuilder createError () {
101114 return createEvent ().type (EventType .ERROR .value ());
102115 }
103116
@@ -108,23 +121,23 @@ public CompletableFuture<Void> submitUnhandledExceptionAsync(
108121 }
109122
110123 public void submitUnhandledException (Exception exception , String submissionMethod ) {
111- Event event = createException ().build ();
124+ Event event = createError ().build ();
112125 PluginContext pluginContext =
113126 PluginContext .builder ()
114127 .exception (exception )
115128 .unhandledError (true )
116129 .submissionMethod (submissionMethod )
117130 .build ();
118- submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
131+ submitEventWithContext (
132+ EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
119133 }
120134
121135 public CompletableFuture <Void > submitFeatureUsageAsync (String feature ) {
122136 return CompletableFuture .runAsync (() -> submitFeatureUsage (feature ), executorService );
123137 }
124138
125139 public void submitFeatureUsage (String feature ) {
126- Event event = createFeatureUsage (feature ).build ();
127- submitEvent (EventPluginContext .from (event ));
140+ submitEvent (createFeatureUsage (feature ).build ());
128141 }
129142
130143 public Event .EventBuilder createFeatureUsage (String feature ) {
@@ -152,8 +165,7 @@ public CompletableFuture<Void> submitLogAsync(String message, String source, Str
152165 }
153166
154167 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 ());
157169 }
158170
159171 public Event .EventBuilder createLog (String message ) {
@@ -166,13 +178,7 @@ public Event.EventBuilder createLog(String message, String source) {
166178
167179 public Event .EventBuilder createLog (String message , String source , String level ) {
168180 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 ();
176182 }
177183
178184 Event .EventBuilder builder =
@@ -184,13 +190,20 @@ public Event.EventBuilder createLog(String message, String source, String level)
184190 return builder .property (EventPropertyKey .LOG_LEVEL .value (), level );
185191 }
186192
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+
187201 public CompletableFuture <Void > submitNotFoundAsync (String resource ) {
188202 return CompletableFuture .runAsync (() -> submitNotFound (resource ), executorService );
189203 }
190204
191205 public void submitNotFound (String resource ) {
192- Event event = createNotFound (resource ).build ();
193- submitEvent (EventPluginContext .from (event ));
206+ submitEvent (createNotFound (resource ).build ());
194207 }
195208
196209 public Event .EventBuilder createNotFound (String resource ) {
@@ -202,8 +215,7 @@ public CompletableFuture<Void> submitSessionStartAsync() {
202215 }
203216
204217 public void submitSessionStart () {
205- Event event = createSessionStart ().build ();
206- submitEvent (EventPluginContext .from (event ));
218+ submitEvent (createSessionStart ().build ());
207219 }
208220
209221 public Event .EventBuilder createSessionStart () {
@@ -216,11 +228,21 @@ public Event.EventBuilder createEvent() {
216228 .date (LocalDate .now ());
217229 }
218230
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 ));
221237 }
222238
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 ) {
224246 eventPluginRunner .run (eventPluginContext );
225247 }
226248
@@ -229,7 +251,7 @@ public CompletableFuture<Void> submitSessionEndAsync(String sessionOrUserId) {
229251 }
230252
231253 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 ));
233255 configurationManager .getSubmissionClient ().sendHeartBeat (sessionOrUserId , true );
234256 }
235257
@@ -247,9 +269,16 @@ public SubmissionResponse updateEmailAndDescription(
247269 .postUserDescription (
248270 referenceId ,
249271 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+ }
250277 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 ()));
253282 }
254283
255284 return response ;
0 commit comments