ArgsConverter extends ClassicConverter { @Override public String convert(ILoggingEvent event) { final Marker marker = event.getMarker(); final Object [] args = event.getArgumentArray(); final int offset = TraceFilter.traceMarkerMap.containsKey(marker) ? 1 : 0; final StringBuilder sb = new StringBuilder(); if (args != null) { if (marker == LogMarker.EXIT.getValue()) { if (args.length > offset) { sb.append(" returns"); String arg = args[offset] == null ? null : args[offset].toString(); if (arg != null && arg.length() > MAX_ARG_LENGTH) arg = arg.substring(0, MAX_ARG_LENGTH)+"..."; sb.append(" ["); sb.append(arg); sb.append("]"); } } else { for (int i = offset; i < args.length; i++) { String arg = args[i] == null ? null : args[i].toString(); if (arg != null && arg.length() > MAX_ARG_LENGTH) arg = arg.substring(0, MAX_ARG_LENGTH)+"..."; sb.append(" ["); sb.append(arg); sb.append("]"); } } } return sb.toString(); }  @Override String convert(ILoggingEvent event);  }
@Test public void testConvertWithUnexpectedEvents() { final ArgsConverter converter = new ArgsConverter(); assertEquals("Unexpected convertion", "", converter.convert(new MockILoggingEvent())); assertEquals("Unexpected convertion", "", converter.convert(new MockILoggingEvent(null, LogMarker.ENTRY.getValue(), "message"))); assertEquals("Unexpected convertion", "", converter.convert(new MockILoggingEvent(null, LogMarker.EXIT.getValue(), "message"))); }
@Test public void testConvertWithExitEvents() { final ArgsConverter converter = new ArgsConverter(); assertEquals("Unexpected convertion", "", converter.convert(new MockILoggingEvent(null, LogMarker.EXIT.getValue(), "message", "arg1"))); assertEquals("Unexpected convertion", " returns [null]", converter.convert(new MockILoggingEvent(null, LogMarker.EXIT.getValue(), "message", "arg1", null))); assertEquals("Unexpected convertion", " returns [arg2]", converter.convert(new MockILoggingEvent(null, LogMarker.EXIT.getValue(), "message", "arg1", "arg2"))); assertEquals("Unexpected convertion", " returns [arg2]", converter.convert(new MockILoggingEvent(null, LogMarker.EXIT.getValue(), "message", "arg1", "arg2", "arg3"))); }
@Test public void testConvertWithNonExitEvents() throws MalformedURLException { final ArgsConverter converter = new ArgsConverter(); assertEquals("Unexpected convertion", "", converter.convert(new MockILoggingEvent(null, LogMarker.ENTRY.getValue(), "message", "arg1"))); assertEquals("Unexpected convertion", " [null]", converter.convert(new MockILoggingEvent(null, LogMarker.DATA.getValue(), "message", "arg1", null))); assertEquals("Unexpected convertion", " [arg2]", converter.convert(new MockILoggingEvent(null, LogMarker.ENTRY.getValue(), "message", "arg1", "arg2"))); assertEquals("Unexpected convertion", " [arg2] [123]", converter.convert(new MockILoggingEvent(null, LogMarker.DATA.getValue(), "message", "arg1", "arg2", 123))); assertEquals("Unexpected convertion", " [45] [foo] [true] [file: converter.convert(new MockILoggingEvent(null, LogMarker.DATA.getValue(), "message", "arg1", 45, "foo", true, new URL("file: assertEquals("Unexpected convertion", " [java.lang.Exception: test exception]", converter.convert(new MockILoggingEvent(null, LogMarker.THROWING.getValue(), "message", "arg1", new Exception("test exception")))); }