@Test public void messageIncludesStackTrace() throws Exception { LogRecord record = new LogRecord(Level.INFO, "message"); record.setLoggerName("logger"); record.setThrown(new Throwable("thrown").fillInStackTrace()); String message = formatter.formatMessage(record); BufferedReader reader = new BufferedReader(new StringReader(message)); assertEquals("logger: message", reader.readLine()); assertEquals("java.lang.Throwable: thrown", reader.readLine()); assertTrue(reader.readLine() .startsWith("\tat " + getClass().getName() + ".messageIncludesStackTrace")); }
@Override public synchronized String formatMessage(LogRecord record) { StringBuilder sb = new StringBuilder(); if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } if (record.getSourceMethodName() != null) { sb.append(' '); sb.append(record.getSourceMethodName()); } sb.append(": "); sb.append(super.formatMessage(record)); Throwable thrown = record.getThrown(); if (thrown != null) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw);) { sb.append("\n"); thrown.printStackTrace(pw); } sb.append(sw.getBuffer()); } return sb.toString(); }
JsonFormatter extends Formatter { @Override public synchronized String formatMessage(LogRecord record) { StringBuilder sb = new StringBuilder(); if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } if (record.getSourceMethodName() != null) { sb.append(' '); sb.append(record.getSourceMethodName()); } sb.append(": "); sb.append(super.formatMessage(record)); Throwable thrown = record.getThrown(); if (thrown != null) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw);) { sb.append("\n"); thrown.printStackTrace(pw); } sb.append(sw.getBuffer()); } return sb.toString(); } }
JsonFormatter extends Formatter { @Override public synchronized String formatMessage(LogRecord record) { StringBuilder sb = new StringBuilder(); if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } if (record.getSourceMethodName() != null) { sb.append(' '); sb.append(record.getSourceMethodName()); } sb.append(": "); sb.append(super.formatMessage(record)); Throwable thrown = record.getThrown(); if (thrown != null) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw);) { sb.append("\n"); thrown.printStackTrace(pw); } sb.append(sw.getBuffer()); } return sb.toString(); }  }
JsonFormatter extends Formatter { @Override public synchronized String formatMessage(LogRecord record) { StringBuilder sb = new StringBuilder(); if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } if (record.getSourceMethodName() != null) { sb.append(' '); sb.append(record.getSourceMethodName()); } sb.append(": "); sb.append(super.formatMessage(record)); Throwable thrown = record.getThrown(); if (thrown != null) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw);) { sb.append("\n"); thrown.printStackTrace(pw); } sb.append(sw.getBuffer()); } return sb.toString(); }  @Override String format(LogRecord record); @Override synchronized String formatMessage(LogRecord record); }
JsonFormatter extends Formatter { @Override public synchronized String formatMessage(LogRecord record) { StringBuilder sb = new StringBuilder(); if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } if (record.getSourceMethodName() != null) { sb.append(' '); sb.append(record.getSourceMethodName()); } sb.append(": "); sb.append(super.formatMessage(record)); Throwable thrown = record.getThrown(); if (thrown != null) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw);) { sb.append("\n"); thrown.printStackTrace(pw); } sb.append(sw.getBuffer()); } return sb.toString(); }  @Override String format(LogRecord record); @Override synchronized String formatMessage(LogRecord record);  }