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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    Log Event Builder - Fluent Builder for LogEvent Construction 日志事件构建器 - LogEvent 的流式构建器
  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    builder(LogLevel level, String message)
    Creates a new builder with the required level and message.
    Returns the value of the callerInfo record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    boolean
    Checks if this event has a marker attached.
    boolean
    Checks if this event has a throwable attached.
    Returns the value of the level record component.
    Returns the value of the loggerName record component.
    Returns the value of the marker record component.
    mdc()
    Returns the value of the mdc record component.
    Returns the value of the message record component.
    Returns the value of the threadName record component.
    Returns the value of the throwable record component.
    long
    Returns the value of the timestampMillis record component.
    Returns a formatted string representation of this log event.
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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

      public static LogEvent.Builder builder(LogLevel level, String message)
      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

      public String toFormattedString()
      Returns a formatted string representation of this log event. 返回此日志事件的格式化字符串表示。

      Format: [timestamp] [thread] LEVEL logger - message

      格式: [时间戳] [线程] 级别 日志记录器 - 消息

      Returns:
      formatted string | 格式化字符串
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • level

      public LogLevel level()
      Returns the value of the level record component.
      Returns:
      the value of the level record component
    • loggerName

      public String loggerName()
      Returns the value of the loggerName record component.
      Returns:
      the value of the loggerName record component
    • message

      public String message()
      Returns the value of the message record component.
      Returns:
      the value of the message record component
    • throwable

      public Throwable throwable()
      Returns the value of the throwable record component.
      Returns:
      the value of the throwable record component
    • marker

      public Marker marker()
      Returns the value of the marker record component.
      Returns:
      the value of the marker record component
    • mdc

      public Map<String,String> mdc()
      Returns the value of the mdc record component.
      Returns:
      the value of the mdc record component
    • timestampMillis

      public long timestampMillis()
      Returns the value of the timestampMillis record component.
      Returns:
      the value of the timestampMillis record component
    • threadName

      public String threadName()
      Returns the value of the threadName record component.
      Returns:
      the value of the threadName record component
    • callerInfo

      public CallerInfo callerInfo()
      Returns the value of the callerInfo record component.
      Returns:
      the value of the callerInfo record component