Class AsyncLogger
java.lang.Object
cloud.opencode.base.log.async.AsyncLogger
- All Implemented Interfaces:
Logger, AutoCloseable
Asynchronous Logger - Offloads log output to a virtual thread via a bounded queue
异步日志记录器 - 通过有界队列将日志输出卸载到虚拟线程
AsyncLogger wraps a delegate Logger and dispatches log messages asynchronously using a single daemon virtual thread that consumes from a bounded blocking queue. When the queue is full, logging falls back to synchronous execution on the caller thread to ensure no messages are dropped or blocked.
AsyncLogger 包装一个委托 Logger,使用单个守护虚拟线程从有界阻塞队列中消费, 异步分发日志消息。当队列满时,日志回退到调用者线程上同步执行,确保消息不会丢失或阻塞。
Features | 主要功能:
- Asynchronous log dispatch via virtual thread - 通过虚拟线程异步分发日志
- Bounded queue with configurable capacity - 可配置容量的有界队列
- Backpressure: synchronous fallback when queue is full - 背压:队列满时同步回退
- MDC context propagation across threads - MDC 上下文跨线程传播
- Flush and graceful shutdown support - 刷新和优雅关闭支持
Usage Examples | 使用示例:
Logger delegate = LoggerFactory.getLogger(MyService.class);
try (AsyncLogger asyncLogger = AsyncLogger.wrap(delegate)) {
asyncLogger.info("This is logged asynchronously");
asyncLogger.flush(); // Wait for all pending messages to be processed
}
// Custom queue capacity
AsyncLogger asyncLogger = AsyncLogger.wrap(delegate, 4096);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: No (delegate must not be null) - 空值安全: 否(委托不能为 null)
- Since:
- JDK 25, opencode-base-log V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this async logger, equivalent toshutdown().voidLogs a message at DEBUG level with a marker.voidLogs a message at DEBUG level with a marker and parameters.voidLogs a message at DEBUG level.voidLogs a message at DEBUG level with parameters.voidLogs a message at DEBUG level with an exception.voidLogs a message at DEBUG level using lazy evaluation.voidLogs a message at ERROR level with a marker.voidLogs a message at ERROR level with a marker and parameters.voidLogs a message at ERROR level with a marker and exception.voidLogs a message at ERROR level.voidLogs a message at ERROR level with parameters.voidLogs a message at ERROR level with an exception.voidLogs an exception at ERROR level.voidLogs a message at ERROR level using lazy evaluation.voidLogs a message at ERROR level using lazy evaluation with exception.voidflush()Blocks until all currently queued log tasks have been processed, or timeout expires.getName()Returns the name of this logger.voidLogs a message at INFO level with a marker.voidLogs a message at INFO level with a marker and parameters.voidLogs a message at INFO level.voidLogs a message at INFO level with parameters.voidLogs a message at INFO level with an exception.voidLogs a message at INFO level using lazy evaluation.booleanChecks if DEBUG level is enabled.booleanisDebugEnabled(Marker marker) Checks if DEBUG level is enabled for the specified marker.booleanChecks if the specified level is enabled.booleanChecks if ERROR level is enabled.booleanisErrorEnabled(Marker marker) Checks if ERROR level is enabled for the specified marker.booleanChecks if INFO level is enabled.booleanisInfoEnabled(Marker marker) Checks if INFO level is enabled for the specified marker.booleanChecks if TRACE level is enabled.booleanisTraceEnabled(Marker marker) Checks if TRACE level is enabled for the specified marker.booleanChecks if WARN level is enabled.booleanisWarnEnabled(Marker marker) Checks if WARN level is enabled for the specified marker.voidLogs a message at the specified level.voidLogs a message at the specified level with parameters.voidLogs a message at the specified level with an exception.voidshutdown()Initiates graceful shutdown.voidLogs a message at TRACE level with a marker.voidLogs a message at TRACE level with a marker and parameters.voidLogs a message at TRACE level.voidLogs a message at TRACE level with parameters.voidLogs a message at TRACE level with an exception.voidLogs a message at TRACE level using lazy evaluation.voidLogs a message at WARN level with a marker.voidLogs a message at WARN level with a marker and parameters.voidLogs a message at WARN level with a marker and exception.voidLogs a message at WARN level.voidLogs a message at WARN level with parameters.voidLogs a message at WARN level with an exception.voidLogs a message at WARN level using lazy evaluation.static AsyncLoggerWraps the given logger with async dispatch using the default queue capacity (8192).static AsyncLoggerWraps the given logger with async dispatch using the specified queue capacity.
-
Method Details
-
wrap
Wraps the given logger with async dispatch using the default queue capacity (8192). 使用默认队列容量(8192)将给定日志记录器包装为异步分发。- Parameters:
delegate- the delegate logger - 委托日志记录器- Returns:
- a new AsyncLogger instance - 新的 AsyncLogger 实例
- Throws:
NullPointerException- if delegate is null - 如果委托为 null
-
wrap
Wraps the given logger with async dispatch using the specified queue capacity. 使用指定队列容量将给定日志记录器包装为异步分发。- Parameters:
delegate- the delegate logger - 委托日志记录器queueCapacity- the queue capacity - 队列容量- Returns:
- a new AsyncLogger instance - 新的 AsyncLogger 实例
- Throws:
NullPointerException- if delegate is null - 如果委托为 nullIllegalArgumentException- if queueCapacity is not positive - 如果队列容量不为正数
-
flush
Blocks until all currently queued log tasks have been processed, or timeout expires. 阻塞直到所有当前排队的日志任务被处理,或超时到期。- Throws:
InterruptedException- if the current thread is interrupted while waiting 如果当前线程在等待时被中断
-
shutdown
public void shutdown()Initiates graceful shutdown. Waits up to 5 seconds for queue drain, then interrupts. 启动优雅关闭。等待最多 5 秒让队列清空,然后中断。 -
close
public void close()Closes this async logger, equivalent toshutdown(). 关闭此异步日志记录器,等同于shutdown()。- Specified by:
closein interfaceAutoCloseable
-
getName
-
isTraceEnabled
public boolean isTraceEnabled()Checks if TRACE level is enabled. 检查 TRACE 级别是否启用。- Specified by:
isTraceEnabledin interfaceLogger- Returns:
- true if enabled - 如果启用返回 true
-
isTraceEnabled
Checks if TRACE level is enabled for the specified marker. 检查指定标记的 TRACE 级别是否启用。- Specified by:
isTraceEnabledin interfaceLogger- Parameters:
marker- the marker - 标记- Returns:
- true if enabled - 如果启用返回 true
-
isDebugEnabled
public boolean isDebugEnabled()Checks if DEBUG level is enabled. 检查 DEBUG 级别是否启用。- Specified by:
isDebugEnabledin interfaceLogger- Returns:
- true if enabled - 如果启用返回 true
-
isDebugEnabled
Checks if DEBUG level is enabled for the specified marker. 检查指定标记的 DEBUG 级别是否启用。- Specified by:
isDebugEnabledin interfaceLogger- Parameters:
marker- the marker - 标记- Returns:
- true if enabled - 如果启用返回 true
-
isInfoEnabled
public boolean isInfoEnabled()Checks if INFO level is enabled. 检查 INFO 级别是否启用。- Specified by:
isInfoEnabledin interfaceLogger- Returns:
- true if enabled - 如果启用返回 true
-
isInfoEnabled
Checks if INFO level is enabled for the specified marker. 检查指定标记的 INFO 级别是否启用。- Specified by:
isInfoEnabledin interfaceLogger- Parameters:
marker- the marker - 标记- Returns:
- true if enabled - 如果启用返回 true
-
isWarnEnabled
public boolean isWarnEnabled()Checks if WARN level is enabled. 检查 WARN 级别是否启用。- Specified by:
isWarnEnabledin interfaceLogger- Returns:
- true if enabled - 如果启用返回 true
-
isWarnEnabled
Checks if WARN level is enabled for the specified marker. 检查指定标记的 WARN 级别是否启用。- Specified by:
isWarnEnabledin interfaceLogger- Parameters:
marker- the marker - 标记- Returns:
- true if enabled - 如果启用返回 true
-
isErrorEnabled
public boolean isErrorEnabled()Checks if ERROR level is enabled. 检查 ERROR 级别是否启用。- Specified by:
isErrorEnabledin interfaceLogger- Returns:
- true if enabled - 如果启用返回 true
-
isErrorEnabled
Checks if ERROR level is enabled for the specified marker. 检查指定标记的 ERROR 级别是否启用。- Specified by:
isErrorEnabledin interfaceLogger- Parameters:
marker- the marker - 标记- Returns:
- true if enabled - 如果启用返回 true
-
isEnabled
-
trace
-
trace
-
trace
-
trace
-
trace
-
trace
-
debug
-
debug
-
debug
-
debug
-
debug
-
debug
-
info
-
info
-
info
-
info
-
info
-
info
-
warn
-
warn
-
warn
-
warn
-
warn
-
warn
-
warn
-
error
-
error
-
error
-
error
-
error
-
error
-
error
-
error
-
error
-
log
-
log
-
log
-