ActionLog { public void context(String key, Object... values) { List<String> contextValues = context.computeIfAbsent(key, k -> new ArrayList<>(Math.max(2, values.length))); for (Object value : values) { String contextValue = String.valueOf(value); if (contextValue.length() > MAX_CONTEXT_VALUE_LENGTH) { throw new Error(format("context value is too long, key={}, value={}...(truncated)", key, contextValue.substring(0, MAX_CONTEXT_VALUE_LENGTH))); } contextValues.add(contextValue); add(event("[context] {}={}", key, contextValue)); } } ActionLog(String message, String id); long elapsed(); String errorCode(); void context(String key, Object... values); void stat(String key, double value); int track(String operation, long elapsed, int readEntries, int writeEntries); String correlationId(); void action(String action); final String id; final Map<String, List<String>> context; final Map<String, Double> stats; final Instant date; public boolean trace; public String action; public List<String> correlationIds; public List<String> clients; public List<String> refIds; public boolean suppressSlowSQLWarning; }
@Test void contextValueIsTooLong() { assertThatThrownBy(() -> log.context("key", "x".repeat(ActionLog.MAX_CONTEXT_VALUE_LENGTH + 1))) .isInstanceOf(Error.class) .hasMessageStartingWith("context value is too long"); }
@Test void multipleContextValues() { log.context("key", "value1"); log.context("key", "value2"); assertThat(log.context).containsEntry("key", List.of("value1", "value2")); }