withFields<R> static method

R withFields<R>(
  1. Map<String, Object?> fields,
  2. R body()
)

Runs body with fields added to every record logged inside it, including asynchronous work it starts, and returns its result.

Use it to tag all logs of one request or job, such as with a request id. Nested calls add to the outer fields. Like SLF4J's MDC, but scoped by zones instead of threads.

SpringLog.withFields({'request': id}, () => handle(request));

Implementation

static R withFields<R>(Map<String, Object?> fields, R Function() body) =>
    runZoned(
      body,
      zoneValues: {
        _fieldsKey: {...currentFields, ...fields},
      },
    );