Class RichLogger
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:
- All standard
Loggerfunctionality via delegation - Convenience methods for formatted logging at all log levels
- Lazy evaluation of formatted messages using
Shorts.fs(String, Object...) - Printf-style formatting (rendered via
StringUtils.format(String, Object...))
Format Support:
The formatting methods use printf-style (Formatter) placeholders. MessageFormat-style
(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
See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class java.util.logging.Logger
global, GLOBAL_LOGGER_NAME -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRichLogger(Logger delegate) Protected constructor - wraps an existing logger instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddHandler(Handler handler) static RichLogger.BuilderCreates a builder for the specified class logger name.static RichLogger.BuilderCreates a builder for the specified logger name.Creates a log record capture for testing.captureEvents(Predicate<LogRecord> filter) captureEvents(Level min) voidvoidLogs a CONFIG level message with formatted string.voidLogs a CONFIG level message with formatted string and throwable.voidstatic LogContextcontext()Returns the thread-confinedLogContextwhose entries are auto-attached to everyLogRecordemitted on the current thread while a scope is open.voidvoidLogs a FINE level message with formatted string.voidLogs a FINE level message with formatted string and throwable.voidvoidvoidLogs a FINER level message with formatted string.voidLogs a FINER level message with formatted string and throwable.voidvoidvoidLogs a FINEST level message with formatted string.voidLogs a FINEST level message with formatted string and throwable.voidReturns the wrapped JUL logger, which (unlike this wrapper) is linked into the JUL logger hierarchy.Handler[]getLevel()static RichLoggerCreates a logger for the specified class.static RichLoggerCreates a logger for the specified name.getName()voidvoidLogs an INFO level message with formatted string.voidLogs an INFO level message with formatted string and throwable.voidbooleanisLoggable(Level level) booleanvoidvoidvoidvoidvoidvoidvoidLogs a message at the specified level with formatted string.voidLogs a message at the specified level with formatted string and throwable.voidremoveHandler(Handler handler) voidvoidsetUseParentListeners(boolean useParentListeners) voidvoidLogs a SEVERE level message with formatted string.voidLogs a SEVERE level message with formatted string and throwable.voidvoidvoidLogs a WARNING level message with formatted string.voidLogs a WARNING level message with formatted string and throwable.voidMethods inherited from class java.util.logging.Logger
entering, entering, entering, exiting, exiting, getAnonymousLogger, getAnonymousLogger, getFilter, getGlobal, getLogger, getParent, getResourceBundle, getResourceBundleName, getUseParentHandlers, log, logp, logp, logp, logp, logp, logp, logrb, logrb, logrb, logrb, logrb, logrb, logrb, logrb, setFilter, setParent, setResourceBundle, setUseParentHandlers, throwing
-
Constructor Details
-
RichLogger
Protected constructor - wraps an existing logger instance.- Parameters:
delegate- The underlying logger to delegate to. Must not benull .
-
-
Method Details
-
getLogger
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 underlyingLogger.getLogger(String).- Parameters:
name- The logger name. Must not benull .- Returns:
- A logger instance (cached and reused for the same name).
-
getLogger
Creates a logger for the specified class.- Parameters:
clazz- The class. Must not benull .- Returns:
- A logger instance.
-
context
Returns the thread-confinedLogContextwhose entries are auto-attached to everyLogRecordemitted 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
LogContextsingleton. Nevernull .
-
builder
Creates a builder for the specified logger name.- Parameters:
name- The logger name.- Returns:
- A new builder.
-
builder
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
LogRecordCapturethat 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
-
isUseParentListeners
-
log
-
severe
-
severe
-
warning
-
warning
-
info
-
info
-
config
-
config
-
fine
-
fine
-
finer
-
finer
-
finest
-
finest
-
log
-
log
-
log
-
log
-
log
-
isLoggable
- Overrides:
isLoggablein classLogger
-
getLevel
-
setLevel
-
getDelegate
Returns the wrapped JUL logger, which (unlike this wrapper) is linked into the JUL logger hierarchy.Use it to walk
parentloggers, e.g. to find where a level was configured.- Returns:
- The wrapped logger. Never
null .
-
getName
-
addHandler
- Overrides:
addHandlerin classLogger
-
removeHandler
- Overrides:
removeHandlerin classLogger
-
getHandlers
- Overrides:
getHandlersin classLogger
-
severe
Logs a SEVERE level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
severe
Logs a SEVERE level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
warning
Logs a WARNING level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
warning
Logs a WARNING level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
info
Logs an INFO level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
info
Logs an INFO level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
config
Logs a CONFIG level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
config
Logs a CONFIG level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
fine
Logs a FINE level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
fine
Logs a FINE level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
finer
Logs a FINER level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
finer
Logs a FINER level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
finest
Logs a FINEST level message with formatted string.- Parameters:
pattern- The format pattern.args- The format arguments.
-
finest
Logs a FINEST level message with formatted string and throwable.- Parameters:
thrown- The throwable.pattern- The format pattern.args- The format arguments.
-
logf
Logs a message at the specified level with formatted string.- Parameters:
level- The log level. Must not benull .pattern- The format pattern.args- The format arguments.
-
logf
Logs a message at the specified level with formatted string and throwable.- Parameters:
level- The log level. Must not benull .pattern- The format pattern.thrown- The throwable.args- The format arguments.
-