CompactFormatter implements LogEventFormatter { @Override public String format(Timber.LogEvent logEvent) { StringBuilder buff = new StringBuilder(200); Util.formatTimeISO(logEvent.getTimestamp(), buff); buff.append('\t') .append(logEvent.getHost()) .append('\t') .append(logEvent.getServiceName()) .append('\t') .append(classFromSource(logEvent.getSource())) .append('\t') .append(logEvent.getType()) .append('\t') .append(Util.logLevelNameForValue(logEvent.getLevel())) .append('\t') .append(logEvent.getConsistencyLevel().toString().substring(0,2)) .append('\t'); boolean first = true; for (Timber.Payload payload : logEvent.getPayloadList()) { String s = payload.getPayload().toStringUtf8(); if ("application/java-exception".equals(payload.getContentType())) { int offset = s.indexOf('\n'); if (offset > 0) { s = s.substring(0, offset); } } buff.append((first?"":" | ")) .append(payload.getName()) .append(": ") .append(Util.escape(s)); first = false; } return buff.toString(); }  @Override String format(Timber.LogEvent logEvent);  }
@Test public void simpleTest() throws Exception { CompactFormatter form = new CompactFormatter(); assertEquals(eventString, form.format(event)); }
@Test (timeout = 1000) public void microBenchmarkTest() { CompactFormatter form = new CompactFormatter(); 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() { CompactFormatter form = new CompactFormatter(); 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)"); }