Skip to content

Structured logging

The logging helpers build on SLF4J 2’s event API. They do not select a logging backend; the application provides one at runtime.

Create a logger for the calling class and attach structured fields with the field infix function:

import org.everbuild.twaddle.core.logging.field
import org.everbuild.twaddle.core.logging.info
import org.everbuild.twaddle.core.logging.logger
private val log = logger()
log.info(
"auth" field "online",
"port" field 25565,
) {
"Test server binding"
}

The message lambda is evaluated only when its level is enabled. trace, debug, info, warn, error, and the general log function accept fields and an optional throwable.

Logger construction is also available as loggerFor<T>() and logger(name).

StructuredLog exposes static overloads accepting either a String or Supplier<String>. Create fields with LogField.field(key, value):

StructuredLog.info(LOGGER, "Test server binding",
LogField.field("auth", "online"),
LogField.field("port", 25565)
);

Configure an SLF4J provider—such as Log4j’s SLF4J implementation—in the application, not in the foundation library.