From 0697e5be63a7fb584af1b4ea1988c06c0dc66971 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:20:23 +0100 Subject: [PATCH 1/7] be: put the X-Request-Id MONETA's support asks for into failures, and say how to log each call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A failure did not carry X-Request-Id; only the DEBUG line had it, and that is off in production. The message now names it, as in "GET /…/accounts?size=100&page=0 (X-Request-Id 7f3c…) answered 503". The DEBUG line was documented for Micronaut only. usage.md says what it holds and how to switch it on per stack, each module README gives its setting, and testing/ checks each setting brings the line into the framework's log. --- .docs/usage.md | 31 +++++++++++++++ llms.txt | 2 +- modules/moneta-api-play/README.md | 9 +++++ modules/moneta-api-quarkus/README.md | 9 +++++ .../moneta-api-spring-boot-starter/README.md | 9 +++++ modules/moneta-api/README.md | 3 +- .../java/cz/insuit/moneta/MonetaClient.java | 16 ++++---- .../cz/insuit/moneta/MonetaException.java | 3 +- .../cz/insuit/moneta/MonetaClientTest.java | 19 ++++++++++ testing/README.md | 4 ++ testing/play/2.9/build.gradle.kts | 7 +++- testing/play/3.0-scala-2.13/build.gradle.kts | 7 +++- testing/play/3.0-scala-3/build.gradle.kts | 7 +++- testing/play/build.gradle.kts | 1 + .../java/example/play/MonetaInPlayTest.java | 38 +++++++++++++++++++ .../shared/src/test/resources/logback.xml | 12 ++++++ .../test/java/example/quarkus/FakeMoneta.java | 5 ++- .../example/quarkus/MonetaInQuarkusTest.java | 38 +++++++++++++++++++ .../example/boot/MonetaInSpringBootTest.java | 23 +++++++++-- 19 files changed, 224 insertions(+), 19 deletions(-) create mode 100644 testing/play/shared/src/test/resources/logback.xml diff --git a/.docs/usage.md b/.docs/usage.md index 084166a..35c26e3 100644 --- a/.docs/usage.md +++ b/.docs/usage.md @@ -130,6 +130,37 @@ A sealed `MonetaException`, so a `switch` over it is exhaustive: An interrupted thread throws `CancellationException` instead, interrupt flag kept. +Each message names the request and, when MONETA sent one, its `X-Request-Id`, which MONETA's support asks for +about one call: `GET /api/v4/vip/aisp/my/accounts?size=100&page=0 (X-Request-Id 7f3c…) answered 503`. Never the +token, nor the body; `body()` has the start of it. + +## Logging + +The client writes one DEBUG line per call, as JSON, to the logger `cz.insuit.moneta.MonetaClient` through +`System.Logger`. Never the token, never the body. Nothing above DEBUG: a failure is thrown, and the application +logs it. + +```json +{"request":"GET /api/v4/vip/aisp/my/accounts?size=100&page=0","status":200,"millis":143,"requestId":"7f3c…","error":null} +``` + +`error` is set when no answer came, and `status` is then `null`. To turn it on: + +| Stack | Set | +|-------|-----| +| Spring Boot | `logging.level.cz.insuit.moneta=DEBUG` | +| Quarkus | `quarkus.log.category."cz.insuit.moneta".level=DEBUG` | +| Play | `` in `logback.xml` | +| Micronaut | `logger.levels.cz.insuit.moneta: DEBUG`, plus `slf4j-jdk-platform-logging` ([why](../modules/moneta-api-micronaut/README.md#logging-and-failures)) | +| SLF4J without a framework | `slf4j-jdk-platform-logging` on the classpath, then the logger at DEBUG in your config | +| nothing but the JDK | the `java.util.logging` file below, with `-Djava.util.logging.config.file=logging.properties` | + +```properties +handlers = java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level = FINE +cz.insuit.moneta.level = FINE +``` + ## Settings `MonetaClient.builder()`: `baseUri(…)`, `requestTimeout(…)`, `partner(…)` (headers MONETA does not ask for yet), diff --git a/llms.txt b/llms.txt index 0b883a6..75fa9ae 100644 --- a/llms.txt +++ b/llms.txt @@ -32,7 +32,7 @@ Mistakes to avoid: ## Docs - [Installing](https://github.com/landsman/moneta-api-java/blob/main/.docs/install.md): the repository block and the token, for Gradle and Maven -- [Using the client](https://github.com/landsman/moneta-api-java/blob/main/.docs/usage.md): calls, filtering, more than one account, failures +- [Using the client](https://github.com/landsman/moneta-api-java/blob/main/.docs/usage.md): calls, filtering, more than one account, failures, logging - [How MONETA API behaves](https://github.com/landsman/moneta-api-java/blob/main/.docs/moneta-api.md): pending and booked movements, dates, ids, errors ## Modules diff --git a/modules/moneta-api-play/README.md b/modules/moneta-api-play/README.md index 46e85fe..86f4c9c 100644 --- a/modules/moneta-api-play/README.md +++ b/modules/moneta-api-play/README.md @@ -198,6 +198,15 @@ To build the client entirely yourself, disable the module with close the client in a stop hook on `play.api.inject.ApplicationLifecycle`, which every application has, unlike `play.inject`'s, bound only with play-java. +## Logging + +One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +([what it holds](../../.docs/usage.md#logging)). Play routes it into Logback; add the logger to `conf/logback.xml`: + +```xml + +``` + ## Jackson Play 3.0 and 2.9 pin Jackson 2.14.3, and the client needs `jackson-annotations` 2.21 or newer; sbt and Gradle both diff --git a/modules/moneta-api-quarkus/README.md b/modules/moneta-api-quarkus/README.md index 1e8ef2d..1cbd4d8 100644 --- a/modules/moneta-api-quarkus/README.md +++ b/modules/moneta-api-quarkus/README.md @@ -155,6 +155,15 @@ implementation("io.micrometer:micrometer-java11") `HttpClientDecorator` of its own instead. It is off under the OpenTelemetry Java agent too, which instruments the JDK client already. Traces come from Quarkus's own OpenTelemetry, not from here. +## Logging + +One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +([what it holds](../../.docs/usage.md#logging)). It lands in Quarkus's log as any category does: + +```properties +quarkus.log.category."cz.insuit.moneta".level=DEBUG +``` + ## Native image Works, with one line in the application's build: Quarkus's BOM pins `jackson-annotations` 2.21, and the client's diff --git a/modules/moneta-api-spring-boot-starter/README.md b/modules/moneta-api-spring-boot-starter/README.md index 7062dff..a9bf2fb 100644 --- a/modules/moneta-api-spring-boot-starter/README.md +++ b/modules/moneta-api-spring-boot-starter/README.md @@ -114,6 +114,15 @@ A `MonetaClient` bean of the application's own replaces the auto-configured one. stops the application at startup, not at the first call. IntelliJ completes the properties from the metadata the starter ships. +## Logging + +One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +([what it holds](../../.docs/usage.md#logging)). Spring Boot routes it into Logback; switch it on as any logger: + +```properties +logging.level.cz.insuit.moneta=DEBUG +``` + --- ## Optional properties diff --git a/modules/moneta-api/README.md b/modules/moneta-api/README.md index 62370c3..3ce4d59 100644 --- a/modules/moneta-api/README.md +++ b/modules/moneta-api/README.md @@ -54,4 +54,5 @@ try (var moneta = new MonetaClient()) { // one per application; close it when th } ``` -The rest is in [Using the client](../../.docs/usage.md). +The rest is in [Using the client](../../.docs/usage.md), including the +[DEBUG line it logs](../../.docs/usage.md#logging) per call. diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java index c34c749..af7a26d 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java @@ -381,16 +381,14 @@ private T get(AccessToken token, String what, String pathAndQuery, Class throw cancelled; } int status = response.statusCode(); - var message = what + " answered " + status; - // X-Request-Id is what MONETA's support asks for about one call. Never the token, never the body. - log( - what, - status, - millisSince(started), - response.headers().firstValue("X-Request-Id").orElse(null), - null); + // X-Request-Id is what MONETA's support asks for about one call, so a failure names it too, for an + // application that logs the exception with DEBUG off. Never the token, never the body. + var requestId = response.headers().firstValue("X-Request-Id").orElse(null); + var call = requestId == null ? what : what + " (X-Request-Id " + requestId + ")"; + var message = call + " answered " + status; + log(what, status, millisSince(started), requestId, null); return switch (status / 100) { - case 2 -> parse(what, () -> JSON.readValue(response.body(), type)); + case 2 -> parse(call, () -> JSON.readValue(response.body(), type)); default -> throw switch (status) { case 401, 403 -> new MonetaException.InvalidToken(message, status, excerpt(response)); diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaException.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaException.java index a228e1f..96e93b8 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaException.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaException.java @@ -5,7 +5,8 @@ /** * A call to MONETA that did not return what was asked for, one subtype per thing a caller does about it, so - * a {@code switch} over them is exhaustive. The message never carries the token, nor MONETA's response body. + * a {@code switch} over them is exhaustive. The message names the request and, when MONETA sent one, its + * {@code X-Request-Id}, which its support asks for; never the token, nor MONETA's response body. * *

The subtypes' constructors are public so a caller's own tests can throw them from a fake; the hierarchy * stays closed. diff --git a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java index 7a5146e..8211d84 100644 --- a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java +++ b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java @@ -900,6 +900,25 @@ void aMaintenanceAnswerSaysWhenToComeBack() { assertEquals(Duration.ofMinutes(2), failed.retryAfter()); } + @Test + void aFailureNamesTheRequestIdMonetasSupportAsksFor() { + server.createContext(ACCOUNTS, exchange -> { + exchange.getResponseHeaders().add("X-Request-Id", "req-42"); + respond(exchange, 503, "{}"); + }); + var failed = assertThrows(MonetaException.HttpError.class, () -> client.accounts(TOKEN)); + assertTrue(String.valueOf(failed.getMessage()).contains("X-Request-Id req-42"), failed.getMessage()); + + server.removeContext(ACCOUNTS); + server.createContext(ACCOUNTS, exchange -> { + exchange.getResponseHeaders().add("X-Request-Id", "req-43"); + respond(exchange, 200, "not json"); + }); + var unreadable = assertThrows(MonetaException.InvalidResponse.class, () -> client.accounts(TOKEN)); + assertTrue( + String.valueOf(unreadable.getMessage()).contains("X-Request-Id req-43"), unreadable.getMessage()); + } + @Test void logsEachCallWithItsRequestIdButNeverTheToken() { server.createContext(ACCOUNTS, exchange -> { diff --git a/testing/README.md b/testing/README.md index d072651..679be9e 100644 --- a/testing/README.md +++ b/testing/README.md @@ -35,3 +35,7 @@ Both also log the client at DEBUG, since writing that line is the one place it s reaches Logback through `logger.levels` alone; without the provider it does not, Micronaut leaves `java.util.logging` alone. +The other frameworks' projects check the same line against the one setting their module README gives, and fail +without it: `logging.level` in Spring Boot, a logger in `logback.xml` in Play, `quarkus.log.category` in Quarkus. +The Play projects carry `play-logback` for that, as every Play application does. + diff --git a/testing/play/2.9/build.gradle.kts b/testing/play/2.9/build.gradle.kts index 8d2f4f7..96fac4e 100644 --- a/testing/play/2.9/build.gradle.kts +++ b/testing/play/2.9/build.gradle.kts @@ -1,2 +1,7 @@ // The rest is shared, in ../build.gradle.kts. -dependencies { testImplementation("com.typesafe.play:play-guice_2.13:2.9.12") } +dependencies { + testImplementation("com.typesafe.play:play-guice_2.13:2.9.12") + // What every Play application has: the sbt plugin adds it, and it configures Logback from logback.xml. On the + // compile classpath too, for the test to read what reached Logback. + testImplementation("com.typesafe.play:play-logback_2.13:2.9.12") +} diff --git a/testing/play/3.0-scala-2.13/build.gradle.kts b/testing/play/3.0-scala-2.13/build.gradle.kts index 951880c..ebc8222 100644 --- a/testing/play/3.0-scala-2.13/build.gradle.kts +++ b/testing/play/3.0-scala-2.13/build.gradle.kts @@ -1,2 +1,7 @@ // The rest is shared, in ../build.gradle.kts. -dependencies { testImplementation("org.playframework:play-guice_2.13:3.0.12") } +dependencies { + testImplementation("org.playframework:play-guice_2.13:3.0.12") + // What every Play application has: the sbt plugin adds it, and it configures Logback from logback.xml. On the + // compile classpath too, for the test to read what reached Logback. + testImplementation("org.playframework:play-logback_2.13:3.0.12") +} diff --git a/testing/play/3.0-scala-3/build.gradle.kts b/testing/play/3.0-scala-3/build.gradle.kts index bbb7b5b..10e8495 100644 --- a/testing/play/3.0-scala-3/build.gradle.kts +++ b/testing/play/3.0-scala-3/build.gradle.kts @@ -1,2 +1,7 @@ // The rest is shared, in ../build.gradle.kts. -dependencies { testImplementation("org.playframework:play-guice_3:3.0.12") } +dependencies { + testImplementation("org.playframework:play-guice_3:3.0.12") + // What every Play application has: the sbt plugin adds it, and it configures Logback from logback.xml. On the + // compile classpath too, for the test to read what reached Logback. + testImplementation("org.playframework:play-logback_3:3.0.12") +} diff --git a/testing/play/build.gradle.kts b/testing/play/build.gradle.kts index 973bf42..4d3249a 100644 --- a/testing/play/build.gradle.kts +++ b/testing/play/build.gradle.kts @@ -13,6 +13,7 @@ subprojects { named("test") { java.srcDir("../shared/src/test/java") resources.srcDir("../../resources") // one MONETA answer for every testing project + resources.srcDir("../shared/src/test/resources") } } } diff --git a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java index d56af01..db0762a 100644 --- a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java +++ b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java @@ -5,6 +5,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import com.google.inject.ConfigurationException; import com.sun.net.httpserver.HttpServer; import cz.insuit.moneta.AccessToken; @@ -24,6 +28,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; +import org.slf4j.LoggerFactory; import play.Application; import play.inject.guice.GuiceApplicationBuilder; import play.libs.Json; @@ -82,6 +87,39 @@ void aClientTheModuleBuiltReadsTransactions() throws Exception { } } + /** What the module README promises: a logger in logback.xml switches the client's DEBUG line on. */ + @Test + void theClientsDebugLineReachesLogbackThroughLogbackXml() throws Exception { + var app = new GuiceApplicationBuilder() + .configure("moneta.base-uri", base) + .configure("moneta.token", "t") + .build(); + try { + // After the build: Play configures Logback from logback.xml while it builds the application. + var lines = new ListAppender(); + lines.start(); + ((LoggerContext) LoggerFactory.getILoggerFactory()) + .getLogger("cz.insuit.moneta") + .addAppender(lines); + + app.injector() + .instanceOf(MonetaClient.class) + .transactions( + app.injector().instanceOf(AccessToken.class), + "acc-1", + LocalDate.of(2026, 8, 1), + LocalDate.of(2026, 8, 31)); + + assertTrue( + lines.list.stream() + .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) + && line.getLevel() == Level.DEBUG), + "no DEBUG line from the client reached Logback"); + } finally { + stop(app); + } + } + @Test void noTokenNoAccessToken() throws Exception { var app = new GuiceApplicationBuilder().build(); diff --git a/testing/play/shared/src/test/resources/logback.xml b/testing/play/shared/src/test/resources/logback.xml new file mode 100644 index 0000000..0a323e9 --- /dev/null +++ b/testing/play/shared/src/test/resources/logback.xml @@ -0,0 +1,12 @@ + + + + + %-5level %logger{36} - %msg%n + + + + + + + diff --git a/testing/quarkus/shared/src/test/java/example/quarkus/FakeMoneta.java b/testing/quarkus/shared/src/test/java/example/quarkus/FakeMoneta.java index 0474483..757dbee 100644 --- a/testing/quarkus/shared/src/test/java/example/quarkus/FakeMoneta.java +++ b/testing/quarkus/shared/src/test/java/example/quarkus/FakeMoneta.java @@ -37,7 +37,10 @@ public Map start() { "quarkus.moneta.base-uri", "http://" + address.getHostString() + ":" + address.getPort() + "/", "quarkus.moneta.token", - "t"); + "t", + // What the extension README tells an application to set for the client's DEBUG line. + "quarkus.log.category.\"cz.insuit.moneta\".level", + "DEBUG"); } @Override diff --git a/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java b/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java index b0e2e0c..87d91f3 100644 --- a/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java +++ b/testing/quarkus/shared/src/test/java/example/quarkus/MonetaInQuarkusTest.java @@ -1,6 +1,7 @@ package example.quarkus; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import cz.insuit.moneta.AccessToken; import cz.insuit.moneta.MonetaClient; @@ -11,6 +12,11 @@ import java.math.BigDecimal; import java.time.LocalDate; import java.util.Currency; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; import org.junit.jupiter.api.Test; /** @@ -35,4 +41,36 @@ void aClientTheExtensionBuiltReadsTransactions() { new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), transactions.get(0).amount()); } + + /** + * What the extension README promises: quarkus.log.category switches the client's DEBUG line on. Quarkus is the + * java.util.logging implementation, so System.Logger lands in its log without a bridge. + */ + @Test + void theClientsDebugLineReachesTheLogThroughALogCategory() { + var lines = new CopyOnWriteArrayList(); + var handler = new Handler() { + @Override + public void publish(LogRecord record) { + lines.add(record); + } + + @Override + public void flush() {} + + @Override + public void close() {} + }; + var logger = Logger.getLogger(MonetaClient.class.getName()); + logger.addHandler(handler); + try { + moneta.transactions(token, "acc-1", LocalDate.of(2026, 8, 1), LocalDate.of(2026, 8, 31)); + } finally { + logger.removeHandler(handler); + } + + assertTrue( + lines.stream().anyMatch(line -> line.getLevel().intValue() <= Level.FINE.intValue()), + "no DEBUG line from the client reached the log"); + } } diff --git a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java index 4d51971..acbe542 100644 --- a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java +++ b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java @@ -1,7 +1,12 @@ package example.boot; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import com.sun.net.httpserver.HttpServer; import cz.insuit.moneta.AccessToken; import cz.insuit.moneta.MonetaClient; @@ -14,6 +19,7 @@ import java.util.Currency; import java.util.Objects; import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -51,11 +57,16 @@ void aClientTheContextBuiltReadsTransactions() throws IOException { .properties( "moneta.base-uri=" + base, "moneta.token=t", - // Spring Boot routes the client's System.Logger into Logback, so this writes its DEBUG line, - // with - // whatever jackson-annotations the BOM pins. + // What the starter README tells an application to set. The line it switches on is the one + // place the client serialises, with whatever jackson-annotations the BOM pins. "logging.level.cz.insuit.moneta=DEBUG") .run()) { + // After the start: Spring Boot resets Logback while it starts. + var lines = new ListAppender(); + lines.start(); + ((LoggerContext) LoggerFactory.getILoggerFactory()) + .getLogger("cz.insuit.moneta") + .addAppender(lines); var moneta = context.getBean(MonetaClient.class); var token = context.getBean(AccessToken.class); @@ -64,6 +75,12 @@ void aClientTheContextBuiltReadsTransactions() throws IOException { assertEquals( new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), transactions.get(0).amount()); + // Spring Boot bridges java.util.logging, where System.Logger goes, and passes its levels on to it. + assertTrue( + lines.list.stream() + .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) + && line.getLevel() == Level.DEBUG), + "no DEBUG line from the client reached Logback"); } finally { server.stop(0); } From a7513937cab493b4a75376933d8c3c7f1b961ad8 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:44:10 +0100 Subject: [PATCH 2/7] be: check a failure without X-Request-Id names none --- .../src/test/java/cz/insuit/moneta/MonetaClientTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java index 8211d84..a8ed3e8 100644 --- a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java +++ b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java @@ -872,6 +872,8 @@ void reportsAnyOtherFailureWithItsStatus() { answer(ACCOUNTS, 503, "{}"); var failed = assertThrows(MonetaException.HttpError.class, () -> client.accounts(TOKEN)); assertEquals(503, failed.status()); + // MONETA sent no X-Request-Id here, so the message has no empty or "null" one. + assertFalse(String.valueOf(failed.getMessage()).contains("X-Request-Id"), failed.getMessage()); } @Test From 14d281518de7fd1ed26c857bb5cf7ca7d236df54 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:45:23 +0100 Subject: [PATCH 3/7] be: keep an X-Request-Id out of the message unless it is short printable ascii --- .../main/java/cz/insuit/moneta/MonetaClient.java | 11 ++++++++++- .../java/cz/insuit/moneta/MonetaClientTest.java | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java index af7a26d..102085d 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java @@ -21,6 +21,7 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import java.util.regex.Pattern; import org.jspecify.annotations.Nullable; import tools.jackson.databind.DeserializationFeature; import tools.jackson.databind.JsonNode; @@ -46,6 +47,12 @@ public final class MonetaClient implements AutoCloseable { // ponytail: a backstop against a server that never sends an empty page; a million movements is far past it. private static final int MAX_PAGES = 10_000; private static final int MAX_BODY = 2_000; + /** + * An {@code X-Request-Id} the message may carry: short printable ASCII. Anything else did not come from MONETA + * (a proxy, a wrong base URI) and stays out of a message that is meant to be safe to log. + */ + private static final Pattern REQUEST_ID = Pattern.compile("[\\x21-\\x7E]{1,128}"); + private static final JsonMapper JSON = JsonMapper.builder() // An amount is money: 0.1 must stay 0.1, not the nearest double. .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) @@ -384,7 +391,9 @@ private T get(AccessToken token, String what, String pathAndQuery, Class // X-Request-Id is what MONETA's support asks for about one call, so a failure names it too, for an // application that logs the exception with DEBUG off. Never the token, never the body. var requestId = response.headers().firstValue("X-Request-Id").orElse(null); - var call = requestId == null ? what : what + " (X-Request-Id " + requestId + ")"; + var call = requestId == null || !REQUEST_ID.matcher(requestId).matches() + ? what + : what + " (X-Request-Id " + requestId + ")"; var message = call + " answered " + status; log(what, status, millisSince(started), requestId, null); return switch (status / 100) { diff --git a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java index a8ed3e8..65d5546 100644 --- a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java +++ b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java @@ -921,6 +921,19 @@ void aFailureNamesTheRequestIdMonetasSupportAsksFor() { String.valueOf(unreadable.getMessage()).contains("X-Request-Id req-43"), unreadable.getMessage()); } + @Test + void aRequestIdThatIsNotShortPrintableAsciiStaysOutOfTheMessage() { + for (var requestId : List.of("", "a b", "x".repeat(129))) { + server.createContext(ACCOUNTS, exchange -> { + exchange.getResponseHeaders().add("X-Request-Id", requestId); + respond(exchange, 503, "{}"); + }); + var failed = assertThrows(MonetaException.HttpError.class, () -> client.accounts(TOKEN)); + assertFalse(String.valueOf(failed.getMessage()).contains("X-Request-Id"), failed.getMessage()); + server.removeContext(ACCOUNTS); + } + } + @Test void logsEachCallWithItsRequestIdButNeverTheToken() { server.createContext(ACCOUNTS, exchange -> { From ce7aaf373ffb91658217dfd55b5036c28a14e9b5 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:46:42 +0100 Subject: [PATCH 4/7] be: name the X-Request-Id in failures found after the answer, a broken movement or balance too --- .../java/cz/insuit/moneta/MonetaClient.java | 23 +++++++++++++------ .../cz/insuit/moneta/MonetaClientTest.java | 8 ++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java index 102085d..e4b8ced 100644 --- a/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java +++ b/modules/moneta-api/src/main/java/cz/insuit/moneta/MonetaClient.java @@ -228,7 +228,8 @@ private static String normalisedIban(String iban) { public Balance balance(AccessToken token, String accountId) { var path = MonetaPaths.balance(accountId); var what = "GET /" + path; - return Balance.from(what, get(token, what, path, BalancesJson.class)); + var answer = get(token, what, path, BalancesJson.class); + return Balance.from(answer.call(), answer.body()); } /** @@ -242,6 +243,7 @@ public Balance balance(AccessToken token, String accountId) { public Instant tokenExpiry(AccessToken token) { var path = MonetaPaths.TOKEN_INTROSPECT; return get(token, "GET /" + path, path, TokenIntrospectionJson.class) + .body() .expiration() .toInstant(); } @@ -329,16 +331,17 @@ private List readAll( List previous = List.of(); for (int page = 0; page < MAX_PAGES; page++) { var what = "GET /" + sized.with("page", page).appendTo(path); - var body = get(token, what, sized.with("page", page).appendTo(path), pageType); + var answer = get(token, what, sized.with("page", page).appendTo(path), pageType); + var body = answer.body(); var items = body.items(); Integer number = body.pageNumber(); if (number != null && number != page) { throw new MonetaException.InvalidResponse( - "GET /" + path + " asked for page " + page + " and got page " + number, null); + answer.call() + " asked for page " + page + " and got page " + number, null); } if (number == null && !items.isEmpty() && items.equals(previous)) { throw new MonetaException.InvalidResponse( - "GET /" + path + " returned the same items for page " + page + " as for the one before," + answer.call() + " returned the same items for page " + page + " as for the one before," + " so the server likely ignores the page parameter", null); } @@ -348,7 +351,7 @@ private List readAll( } catch (MonetaException.InvalidResponse e) { var reference = node.path("entryReference").asString("?"); throw new MonetaException.InvalidResponse( - what + ", item entryReference=" + reference + ": " + e.getMessage(), e); + answer.call() + ", item entryReference=" + reference + ": " + e.getMessage(), e); } } Integer count = body.pageCount(); @@ -360,8 +363,14 @@ private List readAll( throw new MonetaException.InvalidResponse("GET /" + path + " did not end within " + MAX_PAGES + " pages", null); } + /** + * A body, and the request it answered as a failure about it names it: {@code what}, with MONETA's {@code + * X-Request-Id} when it sent one, for a failure found after {@link #get} returned. + */ + private record Answer(T body, String call) {} + /** {@code what} names the request in messages: the query carries only dates and paging, the token is not in it. */ - private T get(AccessToken token, String what, String pathAndQuery, Class type) { + private Answer get(AccessToken token, String what, String pathAndQuery, Class type) { var request = HttpRequest.newBuilder(baseUri.resolve(pathAndQuery)) .timeout(requestTimeout) .header("Authorization", "Bearer " + token.value()) @@ -397,7 +406,7 @@ private T get(AccessToken token, String what, String pathAndQuery, Class var message = call + " answered " + status; log(what, status, millisSince(started), requestId, null); return switch (status / 100) { - case 2 -> parse(call, () -> JSON.readValue(response.body(), type)); + case 2 -> new Answer<>(parse(call, () -> JSON.readValue(response.body(), type)), call); default -> throw switch (status) { case 401, 403 -> new MonetaException.InvalidToken(message, status, excerpt(response)); diff --git a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java index 65d5546..aead004 100644 --- a/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java +++ b/modules/moneta-api/src/test/java/cz/insuit/moneta/MonetaClientTest.java @@ -455,10 +455,14 @@ void aBalanceWithoutTheAvailableOneIsRefused() { {"amount": {"currency": "CZK", "value": 1}, "creditDebitIndicator": "CRDT", "date": {"dateTime": "2026-11-07T15:32:12Z"}, "type": {"codeOrProprietary": {"code": "CLBD"}}} ]}"""; - answer(BALANCE, 200, bookedOnly); + server.createContext(BALANCE, exchange -> { + exchange.getResponseHeaders().add("X-Request-Id", "req-7"); + respond(exchange, 200, bookedOnly); + }); var refused = assertThrows(MonetaException.InvalidResponse.class, () -> client.balance(TOKEN, "acc-1")); assertTrue(String.valueOf(refused.getMessage()).contains("CLAV"), refused.getMessage()); + assertTrue(String.valueOf(refused.getMessage()).contains("X-Request-Id req-7"), refused.getMessage()); } @Test @@ -821,6 +825,7 @@ void anAccountWithoutACurrencyIsAnInvalidResponse() { void aBrokenItemNamesTheRequestThePageAndTheMovement() { server.createContext(TRANSACTIONS, exchange -> { var first = exchange.getRequestURI().getQuery().endsWith("page=0"); + exchange.getResponseHeaders().add("X-Request-Id", first ? "req-0" : "req-1"); respond(exchange, 200, first ? fixture("transactions-page-0.json") : movement("BOOK", "XXXX")); }); @@ -828,6 +833,7 @@ void aBrokenItemNamesTheRequestThePageAndTheMovement() { MonetaException.InvalidResponse.class, () -> client.transactions(TOKEN, "acc-1", FROM, TO)); var message = Objects.requireNonNull(failed.getMessage()); assertTrue(message.contains("page=1"), message); + assertTrue(message.contains("X-Request-Id req-1"), message); assertTrue(message.contains("entryReference=t"), message); assertTrue(message.contains("'XXXX'"), message); } From 46541e8d6948573f7fba650085efb99119dc763c Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:46:50 +0100 Subject: [PATCH 5/7] docs: say only InvalidToken and HttpError keep the start of the body --- .docs/usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.docs/usage.md b/.docs/usage.md index 35c26e3..693c6a1 100644 --- a/.docs/usage.md +++ b/.docs/usage.md @@ -132,7 +132,7 @@ An interrupted thread throws `CancellationException` instead, interrupt flag kep Each message names the request and, when MONETA sent one, its `X-Request-Id`, which MONETA's support asks for about one call: `GET /api/v4/vip/aisp/my/accounts?size=100&page=0 (X-Request-Id 7f3c…) answered 503`. Never the -token, nor the body; `body()` has the start of it. +token, nor the body; `body()` on `InvalidToken` and `HttpError` has the start of it. ## Logging From 7c31ebb67155e7474d3d4aec3988b676fb1ea0b5 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:47:11 +0100 Subject: [PATCH 6/7] docs: say one DEBUG line per HTTP request, not per call --- .docs/usage.md | 6 +++--- modules/moneta-api-micronaut/README.md | 2 +- modules/moneta-api-play/README.md | 2 +- modules/moneta-api-quarkus/README.md | 2 +- modules/moneta-api-spring-boot-starter/README.md | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.docs/usage.md b/.docs/usage.md index 693c6a1..0d7fc6f 100644 --- a/.docs/usage.md +++ b/.docs/usage.md @@ -136,9 +136,9 @@ token, nor the body; `body()` on `InvalidToken` and `HttpError` has the start of ## Logging -The client writes one DEBUG line per call, as JSON, to the logger `cz.insuit.moneta.MonetaClient` through -`System.Logger`. Never the token, never the body. Nothing above DEBUG: a failure is thrown, and the application -logs it. +The client writes one DEBUG line per HTTP request, as JSON, to the logger `cz.insuit.moneta.MonetaClient` +through `System.Logger`: a read of several pages makes several, and an interrupted request none. Never the token, +never the body. Nothing above DEBUG: a failure is thrown, and the application logs it. ```json {"request":"GET /api/v4/vip/aisp/my/accounts?size=100&page=0","status":200,"millis":143,"requestId":"7f3c…","error":null} diff --git a/modules/moneta-api-micronaut/README.md b/modules/moneta-api-micronaut/README.md index 667d0a8..306a813 100644 --- a/modules/moneta-api-micronaut/README.md +++ b/modules/moneta-api-micronaut/README.md @@ -146,7 +146,7 @@ HttpClientDecorator noMonetaObservation() { ## Logging and failures -The client writes one DEBUG line per call through `System.Logger`, as `cz.insuit.moneta.MonetaClient`, never +The client writes one DEBUG line per HTTP request through `System.Logger`, as `cz.insuit.moneta.MonetaClient`, never with the token. Unlike Spring Boot and Play, Micronaut with Logback does not route that into Logback by itself: `System.Logger` falls back to `java.util.logging`, which Micronaut leaves alone. Add the SLF4J provider to the application, and the usual level setting then applies: diff --git a/modules/moneta-api-play/README.md b/modules/moneta-api-play/README.md index 86f4c9c..3ca7a12 100644 --- a/modules/moneta-api-play/README.md +++ b/modules/moneta-api-play/README.md @@ -200,7 +200,7 @@ close the client in a stop hook on `play.api.inject.ApplicationLifecycle`, which ## Logging -One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +One DEBUG line per HTTP request, with MONETA's `X-Request-Id` and never the token ([what it holds](../../.docs/usage.md#logging)). Play routes it into Logback; add the logger to `conf/logback.xml`: ```xml diff --git a/modules/moneta-api-quarkus/README.md b/modules/moneta-api-quarkus/README.md index 1cbd4d8..acc21cf 100644 --- a/modules/moneta-api-quarkus/README.md +++ b/modules/moneta-api-quarkus/README.md @@ -157,7 +157,7 @@ JDK client already. Traces come from Quarkus's own OpenTelemetry, not from here. ## Logging -One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +One DEBUG line per HTTP request, with MONETA's `X-Request-Id` and never the token ([what it holds](../../.docs/usage.md#logging)). It lands in Quarkus's log as any category does: ```properties diff --git a/modules/moneta-api-spring-boot-starter/README.md b/modules/moneta-api-spring-boot-starter/README.md index a9bf2fb..e3738ce 100644 --- a/modules/moneta-api-spring-boot-starter/README.md +++ b/modules/moneta-api-spring-boot-starter/README.md @@ -116,7 +116,7 @@ starter ships. ## Logging -One DEBUG line per call, with MONETA's `X-Request-Id` and never the token +One DEBUG line per HTTP request, with MONETA's `X-Request-Id` and never the token ([what it holds](../../.docs/usage.md#logging)). Spring Boot routes it into Logback; switch it on as any logger: ```properties From 3e072d720946e1f3cb5aab144b3c69c235b3acb4 Mon Sep 17 00:00:00 2001 From: Michal Landsman Date: Fri, 20 Nov 2026 09:47:50 +0100 Subject: [PATCH 7/7] be: detach the test's log appender once the check is done --- .../micronaut/MonetaInMicronautTest.java | 20 ++++++---- .../java/example/play/MonetaInPlayTest.java | 38 ++++++++++--------- .../example/boot/MonetaInSpringBootTest.java | 35 +++++++++-------- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java b/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java index a77f46d..289c29f 100644 --- a/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java +++ b/testing/micronaut/shared/src/test/java/example/micronaut/MonetaInMicronautTest.java @@ -89,15 +89,21 @@ void theClientsDebugLineReachesLogbackThroughLoggerLevels() throws IOException { // After the start: Micronaut resets Logback from logback.xml while applying logger.levels. var lines = new ListAppender(); lines.start(); - logback.getLogger("cz.insuit.moneta").addAppender(lines); + var logger = logback.getLogger("cz.insuit.moneta"); + logger.addAppender(lines); - context.getBean(MonetaClient.class).accounts(new AccessToken("t")); + try { + context.getBean(MonetaClient.class).accounts(new AccessToken("t")); - assertTrue( - lines.list.stream() - .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) - && line.getLevel() == Level.DEBUG), - "no DEBUG line from the client reached Logback"); + assertTrue( + lines.list.stream() + .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) + && line.getLevel() == Level.DEBUG), + "no DEBUG line from the client reached Logback"); + } finally { + logger.detachAppender(lines); + lines.stop(); + } } finally { server.stop(0); } diff --git a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java index db0762a..9bec6a8 100644 --- a/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java +++ b/testing/play/shared/src/test/java/example/play/MonetaInPlayTest.java @@ -98,23 +98,27 @@ void theClientsDebugLineReachesLogbackThroughLogbackXml() throws Exception { // After the build: Play configures Logback from logback.xml while it builds the application. var lines = new ListAppender(); lines.start(); - ((LoggerContext) LoggerFactory.getILoggerFactory()) - .getLogger("cz.insuit.moneta") - .addAppender(lines); - - app.injector() - .instanceOf(MonetaClient.class) - .transactions( - app.injector().instanceOf(AccessToken.class), - "acc-1", - LocalDate.of(2026, 8, 1), - LocalDate.of(2026, 8, 31)); - - assertTrue( - lines.list.stream() - .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) - && line.getLevel() == Level.DEBUG), - "no DEBUG line from the client reached Logback"); + var logger = ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger("cz.insuit.moneta"); + logger.addAppender(lines); + + try { + app.injector() + .instanceOf(MonetaClient.class) + .transactions( + app.injector().instanceOf(AccessToken.class), + "acc-1", + LocalDate.of(2026, 8, 1), + LocalDate.of(2026, 8, 31)); + + assertTrue( + lines.list.stream() + .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) + && line.getLevel() == Level.DEBUG), + "no DEBUG line from the client reached Logback"); + } finally { + logger.detachAppender(lines); + lines.stop(); + } } finally { stop(app); } diff --git a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java index acbe542..221ccb8 100644 --- a/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java +++ b/testing/spring-boot/shared/src/test/java/example/boot/MonetaInSpringBootTest.java @@ -64,23 +64,28 @@ void aClientTheContextBuiltReadsTransactions() throws IOException { // After the start: Spring Boot resets Logback while it starts. var lines = new ListAppender(); lines.start(); - ((LoggerContext) LoggerFactory.getILoggerFactory()) - .getLogger("cz.insuit.moneta") - .addAppender(lines); - var moneta = context.getBean(MonetaClient.class); - var token = context.getBean(AccessToken.class); + var logger = ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger("cz.insuit.moneta"); + logger.addAppender(lines); + try { + var moneta = context.getBean(MonetaClient.class); + var token = context.getBean(AccessToken.class); - var transactions = moneta.transactions(token, "acc-1", LocalDate.of(2026, 8, 1), LocalDate.of(2026, 8, 31)); + var transactions = + moneta.transactions(token, "acc-1", LocalDate.of(2026, 8, 1), LocalDate.of(2026, 8, 31)); - assertEquals( - new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), - transactions.get(0).amount()); - // Spring Boot bridges java.util.logging, where System.Logger goes, and passes its levels on to it. - assertTrue( - lines.list.stream() - .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) - && line.getLevel() == Level.DEBUG), - "no DEBUG line from the client reached Logback"); + assertEquals( + new Money(new BigDecimal("-12.50"), Currency.getInstance("CZK")), + transactions.get(0).amount()); + // Spring Boot bridges java.util.logging, where System.Logger goes, and passes its levels on to it. + assertTrue( + lines.list.stream() + .anyMatch(line -> line.getLoggerName().equals(MonetaClient.class.getName()) + && line.getLevel() == Level.DEBUG), + "no DEBUG line from the client reached Logback"); + } finally { + logger.detachAppender(lines); + lines.stop(); + } } finally { server.stop(0); }