diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-13 11:06:09 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-13 11:06:09 +1200 |
commit | d89b2099cc3d4b0dc6e85a83050cb8083156bc88 (patch) | |
tree | 1e2b7520926abc62ff2c252fe67b20bdc6c94c1b /src/org/traccar/helper | |
parent | a5133e91d39db417344cc845c74a79be28ff1130 (diff) | |
download | traccar-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.tar.gz traccar-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.tar.bz2 traccar-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.zip |
Improve stack trace logging
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/Log.java | 61 |
1 files changed, 48 insertions, 13 deletions
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 |