Record Class LogEvent
java.lang.Object
java.lang.Record
cloud.opencode.base.log.LogEvent
public record LogEvent(LogLevel level, String loggerName, String message, Throwable throwable, Marker marker, Map<String,String> mdc, long timestampMillis, String threadName, CallerInfo callerInfo)
extends Record
Log Event Record - Immutable Representation of a Log Entry
日志事件记录 - 日志条目的不可变表示
Encapsulates all information about a single log event, including the log level, message, optional throwable, marker, MDC context, timestamp, thread name, and caller info.
封装单个日志事件的所有信息,包括日志级别、消息、可选异常、标记、MDC 上下文、 时间戳、线程名和调用者信息。
Features | 主要功能:
- Immutable log event representation - 不可变的日志事件表示
- Fluent builder for convenient construction - 流式构建器方便构造
- Defensive copy of MDC context map - MDC 上下文映射的防御性拷贝
- Formatted string output - 格式化字符串输出
Usage Examples | 使用示例:
// Build a log event
LogEvent event = LogEvent.builder(LogLevel.INFO, "User logged in")
.loggerName("com.example.UserService")
.throwable(null)
.build();
// Check event properties
if (event.hasThrowable()) {
// handle throwable
}
// Format for output
String formatted = event.toFormattedString();
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Partially (level and message required) - 空值安全: 部分(level 和 message 必填)
- Since:
- JDK 25, opencode-base-log V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classLog Event Builder - Fluent Builder for LogEvent Construction 日志事件构建器 - LogEvent 的流式构建器 -
Constructor Summary
ConstructorsConstructorDescriptionLogEvent(LogLevel level, String loggerName, String message, Throwable throwable, Marker marker, Map<String, String> mdc, long timestampMillis, String threadName, CallerInfo callerInfo) Compact constructor that validates required fields and makes a defensive copy of the MDC map. -
Method Summary
Modifier and TypeMethodDescriptionstatic LogEvent.BuilderCreates a new builder with the required level and message.Returns the value of thecallerInforecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanChecks if this event has a marker attached.booleanChecks if this event has a throwable attached.level()Returns the value of thelevelrecord component.Returns the value of theloggerNamerecord component.marker()Returns the value of themarkerrecord component.mdc()Returns the value of themdcrecord component.message()Returns the value of themessagerecord component.Returns the value of thethreadNamerecord component.Returns the value of thethrowablerecord component.longReturns the value of thetimestampMillisrecord component.Returns a formatted string representation of this log event.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
LogEvent
public LogEvent(LogLevel level, String loggerName, String message, Throwable throwable, Marker marker, Map<String, String> mdc, long timestampMillis, String threadName, CallerInfo callerInfo) Compact constructor that validates required fields and makes a defensive copy of the MDC map. 紧凑构造函数,验证必填字段并对 MDC 映射进行防御性拷贝。- Parameters:
level- the log level | 日志级别loggerName- the logger name | 日志记录器名称message- the log message | 日志消息throwable- the optional throwable | 可选异常marker- the optional marker | 可选标记mdc- the MDC context map (defensively copied) | MDC 上下文映射(防御性拷贝)timestampMillis- the event timestamp in milliseconds | 事件时间戳(毫秒)threadName- the thread name | 线程名callerInfo- the caller info | 调用者信息
-
-
Method Details
-
builder
Creates a new builder with the required level and message. 使用必填的级别和消息创建新的构建器。- Parameters:
level- the log level | 日志级别message- the log message | 日志消息- Returns:
- a new builder | 新的构建器
-
hasThrowable
public boolean hasThrowable()Checks if this event has a throwable attached. 检查此事件是否附带异常。- Returns:
- true if a throwable is present | 如果有异常则返回 true
-
hasMarker
public boolean hasMarker()Checks if this event has a marker attached. 检查此事件是否附带标记。- Returns:
- true if a marker is present | 如果有标记则返回 true
-
toFormattedString
Returns a formatted string representation of this log event. 返回此日志事件的格式化字符串表示。Format:
[timestamp] [thread] LEVEL logger - message格式:
[时间戳] [线程] 级别 日志记录器 - 消息- Returns:
- formatted string | 格式化字符串
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
level
Returns the value of thelevelrecord component.- Returns:
- the value of the
levelrecord component
-
loggerName
Returns the value of theloggerNamerecord component.- Returns:
- the value of the
loggerNamerecord component
-
message
Returns the value of themessagerecord component.- Returns:
- the value of the
messagerecord component
-
throwable
Returns the value of thethrowablerecord component.- Returns:
- the value of the
throwablerecord component
-
marker
Returns the value of themarkerrecord component.- Returns:
- the value of the
markerrecord component
-
mdc
-
timestampMillis
public long timestampMillis()Returns the value of thetimestampMillisrecord component.- Returns:
- the value of the
timestampMillisrecord component
-
threadName
Returns the value of thethreadNamerecord component.- Returns:
- the value of the
threadNamerecord component
-
callerInfo
Returns the value of thecallerInforecord component.- Returns:
- the value of the
callerInforecord component
-