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 +++++++++++++++++----- .../org/traccar/helper/DIstanceCalculatorTest.java | 14 ----- .../org/traccar/helper/DistanceCalculatorTest.java | 14 +++++ test/org/traccar/helper/LogTest.java | 13 +++++ 4 files changed, 75 insertions(+), 27 deletions(-) delete mode 100644 test/org/traccar/helper/DIstanceCalculatorTest.java create mode 100644 test/org/traccar/helper/DistanceCalculatorTest.java create mode 100644 test/org/traccar/helper/LogTest.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 diff --git a/test/org/traccar/helper/DIstanceCalculatorTest.java b/test/org/traccar/helper/DIstanceCalculatorTest.java deleted file mode 100644 index 293cbb114..000000000 --- a/test/org/traccar/helper/DIstanceCalculatorTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.traccar.helper; - -import org.junit.Assert; -import org.junit.Test; - -public class DIstanceCalculatorTest { - - @Test - public void testDistance() { - Assert.assertEquals( - DistanceCalculator.distance(0.0, 0.0, 0.05, 0.05), 7863.0, 10.0); - } - -} diff --git a/test/org/traccar/helper/DistanceCalculatorTest.java b/test/org/traccar/helper/DistanceCalculatorTest.java new file mode 100644 index 000000000..7bbb4e3b1 --- /dev/null +++ b/test/org/traccar/helper/DistanceCalculatorTest.java @@ -0,0 +1,14 @@ +package org.traccar.helper; + +import org.junit.Assert; +import org.junit.Test; + +public class DistanceCalculatorTest { + + @Test + public void testDistance() { + Assert.assertEquals( + DistanceCalculator.distance(0.0, 0.0, 0.05, 0.05), 7863.0, 10.0); + } + +} 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"))); + } + +} -- cgit v1.2.3