DefaultStacktraceConverter extends StacktraceConverter { @Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } } DefaultStacktraceConverter();   DefaultStacktraceConverter(int maxSize); @Override void convert(Throwable t, StringBuilder appendTo); static final int MAX_SIZE; }
@Test public void compareCompleteStacktraceWithExpectation() throws InterruptedException { Thread thread = new Thread(new Runnable() { @Override public void run() { DefaultStacktraceConverter converter = new DefaultStacktraceConverter(4000); StacktraceGenerator generator = new StacktraceGenerator(5, 10, 5); converter.convert(generator.generateException(), stringBuilder); } }); thread.start(); thread.join(); expectedString = "[\"java.lang.IllegalArgumentException: too long\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.generateException(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.converter.DefaultStacktraceConverterTest$X.run(DefaultStacktraceConverterTest.java:X)\"," + "\"\\tat " + getThreadClassName() + ".run(Thread.java:X)\"]"; Assert.assertEquals(expectedString, stringBuilder.toString().replaceAll(":\\d+\\)", ":X)"). replaceAll("\\$\\d", "\\$X")); }
@Test public void compareTruncatedStacktraceWithExpectation() throws InterruptedException { Thread thread = new Thread(new Runnable() { @Override public void run() { DefaultStacktraceConverter converter = new DefaultStacktraceConverter(2000); StacktraceGenerator generator = new StacktraceGenerator(5, 20, 5); converter.convert(generator.generateException(), stringBuilder); } }); thread.start(); thread.join(); expectedString = "[\"-------- STACK TRACE TRUNCATED --------\"," + "\"java.lang.IllegalArgumentException: too long\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"-------- OMITTED --------\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.generateException(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.converter.DefaultStacktraceConverterTest$X.run(DefaultStacktraceConverterTest.java:X)\"," + "\"\\tat " + getThreadClassName() + ".run(Thread.java:X)\"]"; Assert.assertEquals(expectedString, stringBuilder.toString().replaceAll(":\\d+\\)", ":X)"). replaceAll("\\$\\d", "\\$X")); }
@Test @Ignore("Use this test for failure analysis of custom stacktraces.") public void customStacktrace() throws Exception { try (InputStream input = getClass().getResourceAsStream("stacktrace.txt"); Reader reader = new InputStreamReader(input); BufferedReader stacktrace = new BufferedReader(reader)) { Throwable exception = Mockito.mock(Throwable.class); Mockito.when(exception.getMessage()).thenReturn("Test-Message"); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { PrintWriter writer = (PrintWriter) invocation.getArguments()[0]; stacktrace.lines().forEach(l -> writer.println(l)); return null; } }).when(exception).printStackTrace(Matchers.any(PrintWriter.class)); DefaultStacktraceConverter converter = new DefaultStacktraceConverter(); StringBuilder sb = new StringBuilder(); converter.convert(exception, sb); System.out.print(sb.toString()); } }