Class LogRecord

java.lang.Object
java.util.logging.LogRecord
org.apache.juneau.commons.logging.LogRecord
All Implemented Interfaces:
Serializable

public class LogRecord extends LogRecord
LogRecord that supports lazy message formatting.

This class extends java.util.logging.LogRecord and overrides getMessage() to use StringUtils.format(String, Object...) (printf-style "%s"/"%d") for lazy formatting. The message is only formatted when getMessage() is actually called.

Usage:

This class is used internally by RichLogger when logging formatted messages. The formatted message is only computed when the LogRecord's message is actually accessed (e.g., by a Handler or Formatter).

Formatting contract:

Juneau RichLogger patterns are printf-style (rendered here via StringUtils.format(String, Object...)). Because this override returns the already-substituted message, downstream Handlers and Formatters that call getMessage() see fully-rendered text and never re-apply formatting — so the printf-vs-MessageFormat choice is purely internal. The JDK's MessageFormat parameter contract of LogRecord only resurfaces if a record is serialized and consumed as a plain LogRecord by a non-Juneau handler; such a consumer would apply MessageFormat to the raw {0} pattern, so printf patterns are not intended for that scenario.

See Also:
See Also:
  • Constructor Details

    • LogRecord

      public LogRecord(String loggerName, Level level, String msg, Object[] parameters, Throwable throwable)
      Constructor.
      Parameters:
      loggerName - The logger name. Can be null.
      level - The log level. Must not be null.
      msg - The message pattern (will be used as-is if args is null or empty).
      parameters - The format arguments, or null if none.
      throwable - The throwable, or null if none.
    • LogRecord

      public LogRecord(String loggerName, Level level, String msg, Object[] parameters, Throwable throwable, MessageGenerator generator)
      Constructor.
      Parameters:
      loggerName - The logger name. Can be null.
      level - The log level. Must not be null.
      msg - The message pattern (will be used as-is if args is null or empty).
      parameters - The format arguments, or null if none.
      throwable - The throwable, or null if none.
      generator - The message generator.
  • Method Details

    • getMessage

      public String getMessage()
      Returns the log message, formatting it lazily if needed.

      If parameters were set via LogRecord.setParameters(Object[]), the message is formatted using StringUtils.format(String, Object...) on first access.

      Overrides:
      getMessage in class LogRecord
      Returns:
      The log message, formatted if parameters are present.
    • formatted

      public String formatted(String format)
      Formats this log record as a string using the specified format pattern.

      Similar to how SimpleFormatter formats log records, this method allows you to specify a custom format string with placeholders that are replaced with actual values from the log record.

      The format string supports both named placeholders (e.g., "{date}") and Formatter-style format specifiers (e.g., "%1$tc"). Named placeholders are replaced first, then the resulting string is formatted using Formatter with the following arguments:

      1. date - The date/time as a Date object
      2. logger - The logger name
      3. level - The log level
      4. message - The formatted log message
      5. thrown - The throwable object (if any)
      Named Placeholders:
      • "{date}" - The date/time formatted using SimpleFormatter's default date format
      • "{timestamp}" - The date/time formatted as ISO-8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
      • "{logger}" - The logger name
      • "{level}" - The log level name
      • "{msg}" - The log message (formatted if parameters are present)
      • "{thread}" - The thread ID
      • "{threadid}" - The thread ID (alias for {thread})
      • "{exception}" - The exception message (if thrown)
      • "{thrown}" - The throwable object (if thrown)
      Formatter-Style Format Specifiers:

      You can use Formatter-style format specifiers directly in the format string. The arguments are passed in the same order as SimpleFormatter:

      • "%1$s" or "%1$tc" - The date/time (argument 1)
      • "%2$s" - The logger name (argument 2)
      • "%3$s" - The log level (argument 3)
      • "%4$s" - The log message (argument 4)
      • "%5$s" - The throwable and its backtrace (argument 5)

      For date/time formatting, you can use date/time conversion characters with argument 1:

      • "%1$tc" - Complete date and time (e.g., "Tue Mar 22 13:11:31 PDT 2011")
      • "%1$tb" - Month abbreviation (e.g., "Mar")
      • "%1$td" - Day of month (01-31)
      • "%1$tY" - Year (4 digits)
      • "%1$tH" - Hour (00-23)
      • "%1$tM" - Minute (00-59)
      • "%1$tS" - Second (00-59)
      • "%1$tL" - Milliseconds (000-999)
      • "%1$tN" - Nanoseconds (000000000-999999999, since JDK 9)
      • "%1$Tp" - AM/PM marker (uppercase)
      • "%1$tp" - AM/PM marker (lowercase)

      The format string can also contain "%n" for newlines.

      Examples:

      LogRecord record = new LogRecord(Level.INFO, "User {0} logged in", "John"); // Using named placeholders String formatted1 = record.formatted("[{timestamp} {level}] {msg}"); // Result: "[2025-01-22T07:42:45.123-0500 INFO] User John logged in" // Using Formatter-style specifiers String formatted2 = record.formatted("%3$s: %4$s [%1$tc]%n"); // Result: "INFO: User John logged in [Tue Jan 22 07:42:45 PST 2025]" // Mixed named placeholders and Formatter specifiers String formatted3 = record.formatted("%1$tb %1$td, %1$tY {level}: {msg}%n"); // Result: "Jan 22, 2025 INFO: User John logged in"

      Parameters:
      format - The format string with placeholders and/or Formatter-style format specifiers.
      Returns:
      The formatted string.
      See Also:
    • formatted

      public static String formatted(LogRecord rec, String format)
      Formats a JUL log record with the same placeholders as formatted(String).
      Parameters:
      rec - The record to format.
      format - The format string.
      Returns:
      The formatted string.