Converter { public Timber.LogEvent convertFrom(LogRecord rec) { Timber.LogEvent.Builder eventBuilder = Timber.LogEvent.newBuilder() .setTimestamp(rec.getMillis()) .setConsistencyLevel(ConsistencyLevel.BESTEFFORT) .setLevel(rec.getLevel().intValue()) .setHost(hostName) .setServiceName(serviceName) .setSource(rec.getSourceClassName() + "#" + rec.getSourceMethodName()) .setPid(0) .setTid(rec.getThreadID()) .setType("T") .addPayload( Timber.Payload.newBuilder() .setName("msg") .setPayload(ByteString.copyFromUtf8(rec.getMessage()))); Throwable cause = rec.getThrown(); if (cause != null) { ByteArrayOutputStream os = new ByteArrayOutputStream(); cause.printStackTrace(new PrintStream(os)); eventBuilder.addPayload( Timber.Payload.newBuilder() .setName("exception") .setContentType("application/java-exception") .setPayload(ByteString.copyFrom(os.toByteArray())) ); } return eventBuilder.build(); } Converter(String serviceName); Timber.LogEvent convertFrom(LogRecord rec);  }
@Test public void testSimple() { Converter conv = new Converter(fakeServiceName); Timber.LogEvent event = conv.convertFrom(rec); assertEquals(rec.getLevel().intValue(), event.getLevel()); assertEquals(rec.getMillis(), event.getTimestamp()); assertEquals(rec.getThreadID(), event.getTid()); assertEquals(rec.getSourceClassName() + "#" + rec.getSourceMethodName(), event.getSource()); assertEquals(0, event.getPid()); assertEquals(fakeServiceName, event.getServiceName()); assertEquals("T", event.getType()); assertEquals(1, event.getPayloadCount()); Timber.Payload payload = event.getPayload(0); assertEquals("msg", payload.getName()); assertEquals(logMessage, payload.getPayload().toStringUtf8()); }
@Test public void testException() { Converter conv = new Converter(fakeServiceName); LogRecord rec = new LogRecord(Level.INFO, "bleh"); rec.setThrown(new RuntimeException("meh")); Timber.LogEvent event = conv.convertFrom(rec); assertEquals("msg", event.getPayload(0).getName()); assertEquals("application/java-exception", event.getPayload(1).getContentType()); }