Skip to content

Commit

Permalink
Default formatter as static var
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n committed Sep 6, 2024
1 parent d6fe249 commit b4bcadb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public interface Formatter {
}

public static class Builder {
public static Formatter DEFAULT_FORMATTER = (req, resp, duration) -> String.format("[%s] %s -> %s (%dms)", req.getPeerAddress(), req, resp, duration);

private Logger logger = LoggerFactory.getLogger(RequestLoggerFilter.class);
private Level logLevel = Level.INFO;
private Formatter msgFormatter = (req, resp, duration) -> String.format("%s -> %s (%dms)", req, resp, duration);
private Formatter msgFormatter = DEFAULT_FORMATTER;

public Builder logger(Logger logger) {
this.logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.concurrent.*;

import static com.mbed.coap.packet.CoapRequest.get;
import static com.mbed.coap.server.filter.RequestLoggerFilter.Builder.DEFAULT_FORMATTER;
import static com.mbed.coap.utils.CoapRequestBuilderFilter.REQUEST_BUILDER_FILTER;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -48,6 +49,13 @@ public void beforeEach() {
listAppender.start();
}

@Test
void defaultFormatterShouldWork() {
String logMsg = DEFAULT_FORMATTER.apply(get("/").address(new InetSocketAddress("1.1.1.1", 31337)).build(), CoapResponse.ok().build(), 123);

assertEquals("[/1.1.1.1:31337] CoapRequest[GET] -> CoapResponse[205] (123ms)", logMsg);
}

@Test
void shouldLogRequestAndResponseWithDefaultMessage() {
// given
Expand All @@ -63,22 +71,21 @@ void shouldLogRequestAndResponseWithDefaultMessage() {
assertTrue(logEvent.getMessage().contains("205"));
}


@Test
void shouldLogRequestAndResponseWithCustomParameters() {
// given
Filter<CoapRequest.Builder, CoapResponse, CoapRequest, CoapResponse> filter = REQUEST_BUILDER_FILTER.andThen(
logFilterBuilder
.msgFormatter((req, resp, __) -> String.format("[%s] %s %s -> %s", req.getPeerAddress(), req.getMethod(), req.options().getUriPath(), resp.getCode().codeToString()))
.msgFormatter((req, resp, __) -> String.format("%s %s -> %s", req.getPeerAddress(), req.getMethod(), req.options().getUriPath(), resp.getCode().codeToString()))
.logLevel(DEBUG)
.build()
);
filter.apply(
get("/dupa").address(new InetSocketAddress("1.1.1.1", 31337)), __ -> CoapResponse.ok().toFuture()
).join();
filter.apply(get("/dupa"), __ -> CoapResponse.ok().toFuture()).join();

// then
ILoggingEvent logEvent = listAppender.list.get(0);
assertEquals(Level.DEBUG, logEvent.getLevel());
assertEquals("[/1.1.1.1:31337] GET /dupa -> 205", logEvent.getMessage());
assertEquals("GET /dupa -> 205", logEvent.getMessage());
}
}

0 comments on commit b4bcadb

Please sign in to comment.