SingleLineFormatter implements LogEventFormatter { @Override public String format(Timber.LogEvent logEvent) { StringBuilder buff = new StringBuilder(200); Util.formatTimeSecondsSinceEpoch(logEvent.getTimestamp(), buff); buff.append('\t') .append(logEvent.getHost()) .append('\t') .append((logEvent.hasPid() ? logEvent.getPid() : "-")) .append("/") .append((logEvent.hasTid() ? logEvent.getTid() : "-")) .append('\t') .append(logEvent.getServiceName()) .append('\t') .append(logEvent.getSource()) .append('\t') .append(logEvent.getType()) .append('\t') .append(Util.logLevelNameForValue(logEvent.getLevel())) .append('\t') .append(logEvent.getConsistencyLevel()) .append('\t'); boolean first = true; for (Timber.Payload payload : logEvent.getPayloadList()) { buff.append((first?"":" | ")) .append(payload.getName()) .append(": ") .append(Util.escape(payload.getPayload().toStringUtf8())); first = false; } return buff.toString(); }  @Override String format(Timber.LogEvent logEvent);  }
@Test public void simpleTest() throws Exception { LogEventFormatter form = new SingleLineFormatter(); assertEquals(eventString, form.format(event)); assertNotNull(form.format(eventWithException)); }
@Test (timeout = 1000) public void microBenchmarkTest() { LogEventFormatter form = new SingleLineFormatter(); int numIterations = 1000; long start = System.currentTimeMillis(); for (int i = 0; i < numIterations; i++) { form.format(event); } long duration = System.currentTimeMillis() - start; double formatsPerSecond = numIterations / ((double) duration / 1000.0); log.info("event formats per second: " + formatsPerSecond + " (" + numIterations + " iterations took " + duration + " milliseconds)"); }
@Test (timeout = 1000) public void microBenchmarkWithExceptionTest() { LogEventFormatter form = new SingleLineFormatter(); int numIterations = 1000; long start = System.currentTimeMillis(); for (int i = 0; i < numIterations; i++) { form.format(eventWithException); } long duration = System.currentTimeMillis() - start; double formatsPerSecond = numIterations / ((double) duration / 1000.0); log.info("event + exception formats per second: " + formatsPerSecond + " (" + numIterations + " iterations took " + duration + " milliseconds)"); }