Friday, February 7, 2014

Java - Exception with Logger

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ExceptionLog {

static void showExceptionLog(String className, String message,
StackTraceElement[] stackTraceElements, Logger logger,
int loggerDepth) {
int logCount;

if (stackTraceElements.length > loggerDepth) {
logCount = loggerDepth;
} else {
logCount = stackTraceElements.length;
}

logger.error("{}: {}", className, message);

for (int i = 1; i <= logCount; i++) {
logger.error(stackTraceElements[stackTraceElements.length - i]
.toString());
}
}
}

public class Test extends Thread {

final Logger logger = LoggerFactory.getLogger(Test.class);

public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
ExceptionLog.showExceptionLog(e.getClass().getName(),
e.getMessage(), e.getStackTrace(), logger, 10);

// TODO Auto-generated catch block
// e.printStackTrace();
}
}

public static void main(String[] args) {
Test test = new Test();

test.start();
test.interrupt();
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.