From d89b2099cc3d4b0dc6e85a83050cb8083156bc88 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 13 Jun 2015 11:06:09 +1200 Subject: Improve stack trace logging --- src/org/traccar/helper/Log.java | 61 ++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'src/org/traccar/helper/Log.java') diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java index 648d4803f..1601994fb 100644 --- a/src/org/traccar/helper/Log.java +++ b/src/org/traccar/helper/Log.java @@ -109,23 +109,12 @@ public class Log { StringBuilder s = new StringBuilder(); if (msg != null) { s.append(msg); - s.append(" - "); } if (exception != null) { - String exceptionMsg = exception.getMessage(); - if (exceptionMsg != null) { - s.append(exceptionMsg); + if (msg != null) { s.append(" - "); } - s.append(exception.getClass().getName()); - StackTraceElement[] stack = exception.getStackTrace(); - if (stack.length > 0) { - s.append(" ("); - s.append(stack[0].getFileName()); - s.append(":"); - s.append(stack[0].getLineNumber()); - s.append(")"); - } + s.append(exception(exception)); } getLogger().warn(s.toString()); } @@ -137,6 +126,52 @@ public class Log { public static void debug(String msg) { getLogger().debug(msg); } + + private static final int MESSAGE_LIMIT = 80; + private static final int STACK_LIMIT = 3; + + public static String exception(Throwable exception) { + StringBuilder s = new StringBuilder(); + String exceptionMsg = exception.getMessage(); + if (exceptionMsg != null) { + s.append(exceptionMsg); + s.append(" - "); + } + s.append(exception.getClass().getSimpleName()); + StackTraceElement[] stack = exception.getStackTrace(); + if (stack.length > 0) { + s.append(" ("); + s.append(stack[0].getFileName()); + s.append(":"); + s.append(stack[0].getLineNumber()); + + if (exceptionMsg == null || exceptionMsg.length() < MESSAGE_LIMIT) { + int count = STACK_LIMIT - 1; + boolean skip = false; + for (int i = 1; i < stack.length; i += 1) { + if (stack[i].getClassName().startsWith("org.traccar")) { + s.append(" < "); + if (skip) { + s.append(" ... < "); + skip = false; + } + s.append(stack[i].getFileName()); + s.append(":"); + s.append(stack[i].getLineNumber()); + count -= 1; + if (count == 0) { + break; + } + } else { + skip = true; + } + } + } + + s.append(")"); + } + return s.toString(); + } /** * Netty logger implementation -- cgit v1.2.3