Interface LogEventBuilder


@Experimental public interface LogEventBuilder
Fluent builder for a single log statement.

Instances are obtained from Logger.atTrace(), Logger.atDebug(), etc. A builder must be consumed in a single chained call; it must not be stored in a variable across statements, as implementations may reuse thread-local state for zero-allocation dispatch.

When the target level is disabled the Logger returns a shared NopLogEventBuilder singleton, making the entire fluent call chain a no-op with zero allocations.


 log.atInfo()
    .withKeyValue("module", module.getId())
    .log("Compiling {} source files", count);
 

Message format

The log(String format, Object...) family of methods uses a simple positional placeholder syntax: each occurrence of {} in the format string is replaced, in order, by the string representation of the corresponding argument. No index, type specifier, or locale conversion is supported — this is intentionally simpler than MessageFormat and avoids the injection risk of printf-style specifiers.

Example: log("Building {} module(s) in {}", count, dir) produces "Building 3 module(s) in /tmp/build" when count=3 and dir="/tmp/build".

This convention is the same as the one used by SLF4J (MessageFormatter), which is widely used in the Java ecosystem and is already the format employed by plugin.Log, the API this interface replaces.

Since:
4.0.0
  • Method Details

    • withKeyValue

      @Nonnull LogEventBuilder withKeyValue(@Nonnull String key, String value)
      Attaches a structured key-value pair to this log event.

      Structured key-value pairs are consumed by structured log appenders (e.g. JSON, ECS, or Logstash layouts in Logback or Log4j 2) that capture fields alongside the message text — rather than embedding metadata inside the message string itself. This enables log aggregators such as Elasticsearch or Splunk to filter and correlate events by field values directly.

      In a Maven context, typical uses include attaching build metadata such as project coordinates, mojo identifiers, and elapsed time as first-class indexed fields. When running with a plain-text appender these pairs are silently ignored or appended after the message, depending on the appender configuration.

      Parameters:
      key - the field name, never null
      value - the string value
      Returns:
      this builder
    • withKeyValue

      @Nonnull LogEventBuilder withKeyValue(@Nonnull String key, long value)
      Attaches a structured key-value pair with a long value.

      See withKeyValue(String, String) for the purpose of structured key-value pairs.

      Parameters:
      key - the field name, never null
      value - the numeric value
      Returns:
      this builder
    • withKeyValue

      @Nonnull LogEventBuilder withKeyValue(@Nonnull String key, boolean value)
      Attaches a structured key-value pair with a boolean value.

      See withKeyValue(String, String) for the purpose of structured key-value pairs.

      Parameters:
      key - the field name, never null
      value - the boolean value
      Returns:
      this builder
    • withThrowable

      Attaches a Throwable to this log event.
      Parameters:
      t - the throwable, may be null (no-op)
      Returns:
      this builder
    • withProjectId

      @Nonnull LogEventBuilder withProjectId(@Nullable String projectId)
      Overrides the project identifier for this event.

      Note: enrichment via this method is best-effort and implementation-defined. Implementations that do not support per-event project context may silently ignore the supplied value.

      Parameters:
      projectId - the project id, or null to clear
      Returns:
      this builder
    • withMojoId

      Overrides the mojo execution identifier for this event.

      Note: enrichment via this method is best-effort and implementation-defined. Implementations that do not support per-event mojo context may silently ignore the supplied value.

      Parameters:
      mojoId - the mojo id, or null to clear
      Returns:
      this builder
    • log

      void log(@Nonnull String message)
      Emits the log event with the given pre-formatted message.
      Parameters:
      message - the message, never null
    • log

      void log(@Nonnull String format, Object arg)
      Emits the log event with a SLF4J-style {} template and a single argument.

      Fast path that avoids varargs array allocation.

      Parameters:
      format - the message template
      arg - the single argument
    • log

      void log(@Nonnull String format, Object arg1, Object arg2)
      Emits the log event with a SLF4J-style {} template and two arguments.

      Fast path that avoids varargs array allocation.

      Parameters:
      format - the message template
      arg1 - the first argument
      arg2 - the second argument
    • log

      void log(@Nonnull String format, Object... args)
      Emits the log event with a SLF4J-style {} template and any number of arguments.
      Parameters:
      format - the message template
      args - the arguments
    • log

      void log(@Nonnull Supplier<String> messageSupplier)
      Emits the log event with a lazily-evaluated message supplier.

      The supplier is only invoked when the level is enabled. When this builder is a NOP instance the supplier is never called.

      Parameters:
      messageSupplier - a supplier of the log message, never null