Class RichLogger

java.lang.Object
java.util.logging.Logger
org.apache.juneau.commons.logging.RichLogger

public class RichLogger extends Logger
Extended logger that provides convenience methods for logging formatted messages.

This class extends Logger and uses delegation internally to wrap an underlying Logger instance. This design provides:

  • Type compatibility - extends RichLogger for use anywhere a RichLogger is expected
  • Flexibility - can wrap existing logger instances
  • Convenience methods for formatted logging using Shorts.fs(String, Object...)
Features:
Format Support:

The formatting methods use printf-style (Formatter) placeholders. MessageFormat-style ("{0}") patterns are not expanded here — use Shorts.mf(String, Object...) / StringUtils.mformat(String, Object...) for those:

  • Printf-style: "%s", "%d", "%1$s", etc.

Juneau RichLogger patterns are printf-style. Because LogRecord.getMessage() returns the already-substituted message, the MessageFormat parameter contract of the underlying LogRecord only surfaces if a record is serialized and consumed as a plain LogRecord by a non-Juneau handler.

Example:

RichLogger logger = RichLogger.getLogger(MyClass.class.getName()); // Formatted logging at different levels (printf-style placeholders) logger.severe("Error processing user %s: %s", userId, error); logger.warning("Failed to connect to %s after %d attempts", hostname, attempts); logger.info("Processing %s items", count); logger.fine("Debug value: %s", debugValue);

See Also:
  • Constructor Details

    • RichLogger

      protected RichLogger(Logger delegate)
      Protected constructor - wraps an existing logger instance.
      Parameters:
      delegate - The underlying logger to delegate to. Must not be null.
  • Method Details

    • getLogger

      public static RichLogger getLogger(String name)
      Creates a logger for the specified name.

      This method returns the same RichLogger instance for a given name, ensuring that listeners attached to a logger persist across multiple calls to getLogger(String). RichLogger instances are cached and automatically created using the underlying Logger.getLogger(String).

      Parameters:
      name - The logger name. Must not be null.
      Returns:
      A logger instance (cached and reused for the same name).
    • getLogger

      public static RichLogger getLogger(Class<?> clazz)
      Creates a logger for the specified class.
      Parameters:
      clazz - The class. Must not be null.
      Returns:
      A logger instance.
    • context

      public static LogContext context()
      Returns the thread-confined LogContext whose entries are auto-attached to every LogRecord emitted on the current thread while a scope is open.

      Repeated calls return the same singleton (there is exactly one thread-local context map; sharing is harmless).

      Returns:
      The shared LogContext singleton. Never null.
    • builder

      public static RichLogger.Builder builder(String name)
      Creates a builder for the specified logger name.
      Parameters:
      name - The logger name.
      Returns:
      A new builder.
    • builder

      public static RichLogger.Builder builder(Class<?> clazz)
      Creates a builder for the specified class logger name.
      Parameters:
      clazz - The class.
      Returns:
      A new builder.
    • captureEvents

      Creates a log record capture for testing.

      This method creates a LogRecordCapture that will capture all log records from this logger. The capture should be closed when done (typically using try-with-resources).

      Returns:
      A LogRecordCapture instance.
    • captureEvents

    • captureEvents

    • setUseParentListeners

      public void setUseParentListeners(boolean useParentListeners)
    • isUseParentListeners

      public boolean isUseParentListeners()
    • log

      public void log(LogRecord rec)
      Overrides:
      log in class Logger
    • severe

      public void severe(String msg)
      Overrides:
      severe in class Logger
    • severe

      public void severe(Supplier<String> msgSupplier)
      Overrides:
      severe in class Logger
    • warning

      public void warning(String msg)
      Overrides:
      warning in class Logger
    • warning

      public void warning(Supplier<String> msgSupplier)
      Overrides:
      warning in class Logger
    • info

      public void info(String msg)
      Overrides:
      info in class Logger
    • info

      public void info(Supplier<String> msgSupplier)
      Overrides:
      info in class Logger
    • config

      public void config(String msg)
      Overrides:
      config in class Logger
    • config

      public void config(Supplier<String> msgSupplier)
      Overrides:
      config in class Logger
    • fine

      public void fine(String msg)
      Overrides:
      fine in class Logger
    • fine

      public void fine(Supplier<String> msgSupplier)
      Overrides:
      fine in class Logger
    • finer

      public void finer(String msg)
      Overrides:
      finer in class Logger
    • finer

      public void finer(Supplier<String> msgSupplier)
      Overrides:
      finer in class Logger
    • finest

      public void finest(String msg)
      Overrides:
      finest in class Logger
    • finest

      public void finest(Supplier<String> msgSupplier)
      Overrides:
      finest in class Logger
    • log

      public void log(Level level, String msg)
      Overrides:
      log in class Logger
    • log

      public void log(Level level, Supplier<String> msgSupplier)
      Overrides:
      log in class Logger
    • log

      public void log(Level level, String msg, Object param1)
      Overrides:
      log in class Logger
    • log

      public void log(Level level, String msg, Object[] params)
      Overrides:
      log in class Logger
    • log

      public void log(Level level, String msg, Throwable thrown)
      Overrides:
      log in class Logger
    • isLoggable

      public boolean isLoggable(Level level)
      Overrides:
      isLoggable in class Logger
    • getLevel

      public Level getLevel()
      Overrides:
      getLevel in class Logger
    • setLevel

      public void setLevel(Level newLevel)
      Overrides:
      setLevel in class Logger
    • getDelegate

      public Logger getDelegate()
      Returns the wrapped JUL logger, which (unlike this wrapper) is linked into the JUL logger hierarchy.

      Use it to walk parent loggers, e.g. to find where a level was configured.

      Returns:
      The wrapped logger. Never null.
    • getName

      public String getName()
      Overrides:
      getName in class Logger
    • addHandler

      public void addHandler(Handler handler)
      Overrides:
      addHandler in class Logger
    • removeHandler

      public void removeHandler(Handler handler)
      Overrides:
      removeHandler in class Logger
    • getHandlers

      public Handler[] getHandlers()
      Overrides:
      getHandlers in class Logger
    • severe

      public void severe(String pattern, Object... args)
      Logs a SEVERE level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • severe

      public void severe(Throwable thrown, String pattern, Object... args)
      Logs a SEVERE level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • warning

      public void warning(String pattern, Object... args)
      Logs a WARNING level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • warning

      public void warning(Throwable thrown, String pattern, Object... args)
      Logs a WARNING level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • info

      public void info(String pattern, Object... args)
      Logs an INFO level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • info

      public void info(Throwable thrown, String pattern, Object... args)
      Logs an INFO level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • config

      public void config(String pattern, Object... args)
      Logs a CONFIG level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • config

      public void config(Throwable thrown, String pattern, Object... args)
      Logs a CONFIG level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • fine

      public void fine(String pattern, Object... args)
      Logs a FINE level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • fine

      public void fine(Throwable thrown, String pattern, Object... args)
      Logs a FINE level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • finer

      public void finer(String pattern, Object... args)
      Logs a FINER level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • finer

      public void finer(Throwable thrown, String pattern, Object... args)
      Logs a FINER level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • finest

      public void finest(String pattern, Object... args)
      Logs a FINEST level message with formatted string.
      Parameters:
      pattern - The format pattern.
      args - The format arguments.
    • finest

      public void finest(Throwable thrown, String pattern, Object... args)
      Logs a FINEST level message with formatted string and throwable.
      Parameters:
      thrown - The throwable.
      pattern - The format pattern.
      args - The format arguments.
    • logf

      public void logf(Level level, String pattern, Object... args)
      Logs a message at the specified level with formatted string.
      Parameters:
      level - The log level. Must not be null.
      pattern - The format pattern.
      args - The format arguments.
    • logf

      public void logf(Level level, String pattern, Throwable thrown, Object... args)
      Logs a message at the specified level with formatted string and throwable.
      Parameters:
      level - The log level. Must not be null.
      pattern - The format pattern.
      thrown - The throwable.
      args - The format arguments.