Class LogRecord
- All Implemented Interfaces:
Serializable
This class extends java.util.logging.LogRecord and overrides getMessage() to use
StringUtils.format(String, Object...) (printf-style 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
See Also:
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFormats this log record as a string using the specified format pattern.static StringFormats a JUL log record with the same placeholders asformatted(String).Returns the log message, formatting it lazily if needed.Methods inherited from class java.util.logging.LogRecord
getInstant, getLevel, getLoggerName, getLongThreadID, getMillis, getParameters, getResourceBundle, getResourceBundleName, getSequenceNumber, getSourceClassName, getSourceMethodName, getThreadID, getThrown, setInstant, setLevel, setLoggerName, setLongThreadID, setMessage, setMillis, setParameters, setResourceBundle, setResourceBundleName, setSequenceNumber, setSourceClassName, setSourceMethodName, setThreadID, setThrown
-
Constructor Details
-
LogRecord
public LogRecord(String loggerName, Level level, String msg, Object[] parameters, Throwable throwable) Constructor.- Parameters:
loggerName- The logger name. Can benull .level- The log level. Must not benull .msg- The message pattern (will be used as-is if args is null or empty).parameters- The format arguments, ornull if none.throwable- The throwable, ornull if none.
-
LogRecord
public LogRecord(String loggerName, Level level, String msg, Object[] parameters, Throwable throwable, MessageGenerator generator) Constructor.- Parameters:
loggerName- The logger name. Can benull .level- The log level. Must not benull .msg- The message pattern (will be used as-is if args is null or empty).parameters- The format arguments, ornull if none.throwable- The throwable, ornull if none.generator- The message generator.
-
-
Method Details
-
getMessage
Returns the log message, formatting it lazily if needed.If parameters were set via
LogRecord.setParameters(Object[]), the message is formatted usingStringUtils.format(String, Object...)on first access.- Overrides:
getMessagein classLogRecord- Returns:
- The log message, formatted if parameters are present.
-
formatted
Formats this log record as a string using the specified format pattern.Similar to how
SimpleFormatterformats 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}" ) andFormatter-style format specifiers (e.g.,"%1$tc" ). Named placeholders are replaced first, then the resulting string is formatted usingFormatterwith the following arguments:date - The date/time as aDateobjectlogger - The logger namelevel - The log levelmessage - The formatted log messagethrown - The throwable object (if any)
Named Placeholders:
"{date}" - The date/time formatted usingSimpleFormatter'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 asSimpleFormatter:"%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 Stringformatted1 =record .formatted("[{timestamp} {level}] {msg}" );// Result: "[2025-01-22T07:42:45.123-0500 INFO] User John logged in" // Using Formatter-style specifiers Stringformatted2 =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 Stringformatted3 =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
Formats a JUL log record with the same placeholders asformatted(String).- Parameters:
rec- The record to format.format- The format string.- Returns:
- The formatted string.
-