Class AbstractLogger
- java.lang.Object
-
- io.microsphere.logging.AbstractLogger
-
- All Implemented Interfaces:
Logger
public abstract class AbstractLogger extends java.lang.Object implements Logger
An abstract implementation of theLogger
interface, providing common functionality for concrete logger implementations.This class ensures consistent handling of logging operations across different logging frameworks by implementing shared logic such as message formatting and exception handling. Subclasses are expected to implement the basic logging methods (e.g.,
Logger.trace(String)
,Logger.debug(String)
, etc.) according to their specific logging backend.Message Formatting
The class supports formatted logging using a syntax similar to SLF4J or Java's
String.format(String, Object...)
. For example:logger.info("User {} logged in from {}", username, ip);
Exception Logging
If the last argument passed to a formatted logging method is an instance of
Throwable
, it will be treated as an exception, and the appropriate method accepting both a message and a throwable will be invoked:try { // some operation that may throw an exception } catch (Exception e) { logger.error("Operation failed", e); }
This behavior is handled internally by the
log(Consumer, BiConsumer, String, Object...)
method.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractLogger(java.lang.String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
debug(java.lang.String format, java.lang.Object... arguments)
Log a message at the DEBUG level according to the specified format and arguments.void
error(java.lang.String format, java.lang.Object... arguments)
Log a message at the ERROR level according to the specified format and arguments.java.lang.String
getName()
Return the name of thisLogger
instance.void
info(java.lang.String format, java.lang.Object... arguments)
Log a message at the INFO level according to the specified format and arguments.protected void
log(java.util.function.Consumer<java.lang.String> messageHandler, java.util.function.BiConsumer<java.lang.String,java.lang.Throwable> messageThrowableHandler, java.lang.String format, java.lang.Object... arguments)
protected java.lang.String
resolveMessage(java.lang.String format, java.lang.Object... arguments)
Resolve the format messagevoid
trace(java.lang.String format, java.lang.Object... arguments)
Log a message at the TRACE level according to the specified format and arguments.void
warn(java.lang.String format, java.lang.Object... arguments)
Log a message at the WARN level according to the specified format and arguments.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.microsphere.logging.Logger
debug, debug, error, error, info, info, isDebugEnabled, isErrorEnabled, isInfoEnabled, isTraceEnabled, isWarnEnabled, trace, trace, warn, warn
-
-
-
-
Method Detail
-
getName
public final java.lang.String getName()
Description copied from interface:Logger
Return the name of thisLogger
instance.
-
trace
public void trace(java.lang.String format, java.lang.Object... arguments)
Description copied from interface:Logger
Log a message at the TRACE level according to the specified format and arguments.This form avoids superfluous string concatenation when the logger is disabled for the TRACE level. However, this variant incurs the hidden (and relatively small) cost of creating an
Object[]
before invoking the method, even if this logger is disabled for TRACE.
-
debug
public void debug(java.lang.String format, java.lang.Object... arguments)
Description copied from interface:Logger
Log a message at the DEBUG level according to the specified format and arguments.
-
info
public void info(java.lang.String format, java.lang.Object... arguments)
Description copied from interface:Logger
Log a message at the INFO level according to the specified format and arguments.
-
warn
public void warn(java.lang.String format, java.lang.Object... arguments)
Description copied from interface:Logger
Log a message at the WARN level according to the specified format and arguments.
-
error
public void error(java.lang.String format, java.lang.Object... arguments)
Description copied from interface:Logger
Log a message at the ERROR level according to the specified format and arguments.
-
log
protected void log(java.util.function.Consumer<java.lang.String> messageHandler, java.util.function.BiConsumer<java.lang.String,java.lang.Throwable> messageThrowableHandler, java.lang.String format, java.lang.Object... arguments)
- Parameters:
messageHandler
- only message to logmessageThrowableHandler
- message andThrowable
to logformat
- the format message or regular messagearguments
- zero or more arguments for the format pattern
-
resolveMessage
protected java.lang.String resolveMessage(java.lang.String format, java.lang.Object... arguments)
Resolve the format message- Parameters:
format
- the format messagearguments
- zero or more arguments for the format pattern- Returns:
- non-null
-
-