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 | |
parent | a5133e91d39db417344cc845c74a79be28ff1130 (diff) | |
download | trackermap-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.tar.gz trackermap-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.tar.bz2 trackermap-server-d89b2099cc3d4b0dc6e85a83050cb8083156bc88.zip |
Improve stack trace logging
-rw-r--r-- | src/org/traccar/helper/Log.java | 61 | ||||
-rw-r--r-- | test/org/traccar/helper/DistanceCalculatorTest.java (renamed from test/org/traccar/helper/DIstanceCalculatorTest.java) | 2 | ||||
-rw-r--r-- | test/org/traccar/helper/LogTest.java | 13 |
3 files changed, 62 insertions, 14 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 diff --git a/test/org/traccar/helper/DIstanceCalculatorTest.java b/test/org/traccar/helper/DistanceCalculatorTest.java index 293cbb114..7bbb4e3b1 100644 --- a/test/org/traccar/helper/DIstanceCalculatorTest.java +++ b/test/org/traccar/helper/DistanceCalculatorTest.java @@ -3,7 +3,7 @@ package org.traccar.helper; import org.junit.Assert; import org.junit.Test; -public class DIstanceCalculatorTest { +public class DistanceCalculatorTest { @Test public void testDistance() { diff --git a/test/org/traccar/helper/LogTest.java b/test/org/traccar/helper/LogTest.java new file mode 100644 index 000000000..08872c1dc --- /dev/null +++ b/test/org/traccar/helper/LogTest.java @@ -0,0 +1,13 @@ +package org.traccar.helper; + +import org.junit.Assert; +import org.junit.Test; + +public class LogTest { + + @Test + public void testLog() { + Assert.assertEquals("test - Exception (LogTest.java:10)", Log.exception(new Exception("test"))); + } + +} |