Skip to content

Logging

Andrea Di Lisio edited this page Aug 29, 2023 · 2 revisions

What logs are available

By default the library does not produce any log. Those can be enabled by invoking the .withHttpLogs(...) upon client creation. As the name of the method suggests, the only logs produced by the library related to HTTP requests and responses to/from our Payments API.

The information logged are safe, that is not including sensitive information.

The following details are shown:

  • Idempotency-Key;
  • HTTP method;
  • HTTP response code;
  • URL;
  • HTTP headers.

Below a sample of the default logs:

TRACE: [HTTP|25241282-a753-444f-984e-fe4563374d2c] --> POST https://api.truelayer-sandbox.com/payments headers=[accept-encoding=gzip, authorization=***, connection=Keep-Alive, content-length=456, content-type=application/json; charset=UTF-8, host=api.truelayer-sandbox.com, idempotency-key=25241282-a753-444f-984e-fe4563374d2c, tl-signature=eyJ0bF9oZWFkZXJzIjoiSWRlbXBvdGVuY3ktS2V5IiwidGxfdmVyc2lvbiI6IjIiLCJhbGciOiJFUzUxMiIsImtpZCI6ImMxZGIyYWNiLTgxNjgtNGIwMS1iNjQ5LTFkZjM5YmUwOTliMiJ9..ABpgtDpz7uhI9uu7Ua16QOWNYlOjQHDCe5CzlzuYbOe8U9niOPkGwpc0BxEwab5jjsPuECns7pcQePNPkZ-2itfpAOmVjwfsUe4T4xvQfm5_1zcS17n6-k7MViZ-yjakayeNU24Ze2LnzChMY1VRKT27fNybhz3ig-oGajGncfSM_ZX7, user-agent=truelayer-java/0.6.0]
TRACE: [HTTP|25241282-a753-444f-984e-fe4563374d2c] <-- 201 POST https://api.truelayer-sandbox.com/payments headers=[cf-cache-status=DYNAMIC, cf-ray=6ebdd07cec1a7bcd-LAX, content-type=application/json; charset=utf-8, date=Mon, 14 Mar 2022 14:49:10 GMT, expect-ct=max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct", server=cloudflare, strict-transport-security=max-age=63072000; includeSubdomains; preload, tl-trace-id=7128e218b454821511730423ba4f83f7, x-content-type-options=nosniff]
TRACE: [HTTP|4c212442-0290-4545-b11e-dfbbbbbd361f] --> GET https://api.truelayer-sandbox.com/payments/a231d68f-e9b5-4739-8fff-d3b81c0546b4 headers=[accept-encoding=gzip, authorization=***, connection=Keep-Alive, host=api.truelayer-sandbox.com, idempotency-key=4c212442-0290-4545-b11e-dfbbbbbd361f, user-agent=truelayer-java/0.6.0]
TRACE: [HTTP|4c212442-0290-4545-b11e-dfbbbbbd361f] <-- 200 GET https://api.truelayer-sandbox.com/payments/a231d68f-e9b5-4739-8fff-d3b81c0546b4 headers=[cf-cache-status=DYNAMIC, cf-ray=6ebdd07e4e387bcd-LAX, content-encoding=gzip, content-type=application/json; charset=utf-8, date=Mon, 14 Mar 2022 14:49:11 GMT, expect-ct=max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct", server=cloudflare, strict-transport-security=max-age=63072000; includeSubdomains; preload, tl-trace-id=ce3fc02b428965f2fcd44224334179f5, x-content-type-options=nosniff]

Enabling default logs

The library is equipped with a default logger, based on Tinylog, that can be enabled by invoking the .withHttpLogs() method on the TrueLayer client instance.

Our default logger is configured to output logs to standard out and it uses a separate writing thread. Its full configuration is declared on this properties file.

Custom logging

In many use cases you might want or need custom logs. There are 2 options to customize logs. Either way, our logs won't leak sensitive information.

Overriding Tinylog configurations

Disclaimer: consider this custom approach for testing purposes only! We might in future switch to another logging provider, therefore the following approach might not be working anymore. We won't consider this as a breaking change. We're making this available to ease the integration and testing phase only, please skip to the following chapter if you're looking for a definitive solution to custom logs.

The first option available for custom logging is kind of a Quick and dirty solution: as Tinylog configuration is read from the runtime on which the SDK runs, you can inject custom properties with the help of a tinylog.properties file or via environment variables. You can refer to Tinylog docs for all the available options.

For instance to redirect the output of the library logs onto a file rather than standard out, you can define the following tinylog.properties file into the resources directory of your project.

writer_tl_java_http = file
writer_tl_java_http.file = /Users/andreadilisio/Documents/TrueLayer/Projects/truelayer-java/examples/quarkus-mvc/log.txt
writer_tl_java_http.tag = HTTP
writer_tl_java_http.format = {level}: [{tag}|{context: Idempotency-Key}] {message} 

Custom log implementation

The ideal approach for custom logs involves calling a withHttpLogs(Consumer<String> logConsumer) method on the TrueLayer client instance.

The above method lets you define a consumer function for log messages. Configuring your existing logging framework should be very easy. With reference to our sample project, you can - for instance - define the following:

TrueLayerClient.New()
  ...
  .withHttpLogs(LOG::info)
  .build();

Though this should be supported by the most known logging framework, we strongly suggest to make sure that your log writing is asynchronous or at least extremely fast, so that you won't affect the performance of the SDK.