forked from yeshodha02/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerStackTraceUtil.java
More file actions
59 lines (52 loc) · 1.65 KB
/
LoggerStackTraceUtil.java
File metadata and controls
59 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.edurekademo.utilities;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Class used to log the print stack trace items
public class LoggerStackTraceUtil {
private static final Logger LOG =LoggerFactory.getLogger(LoggerStackTraceUtil.class);
private int maxCount=3;
/**
* @param th - The exception that was thrown and to be logged.
* @return at very least the 1st error, if stacktrace is more than 1, then it also
* returns the immediate cause
*
* Note this function cannot be made static for thread safety..
*/
public String getErrorMessage(Throwable th){
if (th==null) return "";
StringBuilder b = new StringBuilder("");
String [] aryError = ExceptionUtils.getRootCauseStackTrace(th);
b.append(aryError[0].trim());
if (aryError.length >= 2){
b.append(String.format("%nCause:%s",aryError[1].trim()));
}
if (aryError.length >= maxCount){
b.append(String.format("%nCause:%s",aryError[2].trim()));
}
return b.toString();
}
// Static Logger function
public static void printErrorMessage(Throwable th)
{
try{
// log the error caused by
LOG.error("Error Cause: {}",th.getMessage());
// Conditional statement to check the length of the array
int count=0;
for(StackTraceElement stackTrace:th.getStackTrace()){
if(count<=25){
LOG.error("Error Class: {} and Line Number: {}",stackTrace.getClassName(),stackTrace.getLineNumber());
}else{
break;
}
count++;
}
}
catch(Exception e)
{
// log the exception error
LoggerStackTraceUtil.printErrorMessage(e);
}
}
}