Class: LogStruct::Log::Error

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
Interfaces::AdditionalDataField, Interfaces::CommonFields, Interfaces::MessageField, MergeAdditionalDataFields
Defined in:
lib/log_struct/log/error.rb

Overview

Exception log entry for Ruby exceptions with class, message, and backtrace

Constant Summary collapse

ErrorEvent =
T.type_alias {
  Event::Error
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MergeAdditionalDataFields

#merge_additional_data_fields

Methods included from SerializeCommon

#as_json, #serialize_common

Constructor Details

#initialize(source:, event: T.let(Event::Error, ErrorEvent), timestamp:, level: T.let(Level::Error, Level), err_class:, message:, backtrace: nil, additional_data: {}) ⇒ void

Parameters:

  • source (Source)

    Common fields

  • event (ErrorEvent) (defaults to: T.let(Event::Error, ErrorEvent))
  • timestamp (Time)
  • level (Level) (defaults to: T.let(Level::Error, Level))
  • err_class (T.class_of(StandardError))

    Exception-specific fields

  • message (String)
  • backtrace (Array<String>, nil) (defaults to: nil)
  • additional_data (Hash{Symbol => T.untyped}) (defaults to: {})


# File ''

const :source, Source
const :event, ErrorEvent, default: T.let(Event::Error, ErrorEvent)
const :timestamp, Time, factory: -> { Time.now }
const :level, Level, default: T.let(Level::Error, Level)
const :err_class, T.class_of(StandardError)
const :message, String
const :backtrace, T.nilable(T::Array[String]), default: nil
const :additional_data, T::Hash[Symbol, T.untyped], default: {}

Instance Attribute Details

#additional_dataHash{Symbol => T.untyped} (readonly)

Returns the value of prop additional_data.

Returns:

  • (Hash{Symbol => T.untyped})


# File ''

const :additional_data, T::Hash[Symbol, T.untyped], default: {}

#backtraceArray<String>? (readonly)

Returns the value of prop backtrace.

Returns:

  • (Array<String>, nil)


# File ''

const :backtrace, T.nilable(T::Array[String]), default: nil

#err_classT.class_of(StandardError) (readonly)

Exception-specific fields

Returns:

  • (T.class_of(StandardError))


# File ''

const :err_class, T.class_of(StandardError)

#eventErrorEvent (readonly)

Returns the value of prop event.

Returns:



# File ''

const :event, ErrorEvent, default: T.let(Event::Error, ErrorEvent)

#levelLevel (readonly)

Returns the value of prop level.

Returns:



# File ''

const :level, Level, default: T.let(Level::Error, Level)

#messageString (readonly)

Returns the value of prop message.

Returns:

  • (String)


# File ''

const :message, String

#sourceSource (readonly)

Common fields

Returns:



# File ''

const :source, Source

#timestampTime (readonly)

Returns the value of prop timestamp.

Returns:

  • (Time)


# File ''

const :timestamp, Time, factory: -> { Time.now }

Class Method Details

.from_exception(source, ex, additional_data = {}) ⇒ Log::Error

Create an Error log from a Ruby StandardError

Parameters:

  • source (Source)
  • ex (StandardError)
  • additional_data (Hash{Symbol => T.untyped}) (defaults to: {})

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/log_struct/log/error.rb', line 65

def self.from_exception(source, ex, additional_data = {})
  new(
    source: source,
    message: ex.message,
    err_class: ex.class,
    backtrace: ex.backtrace,
    additional_data: additional_data
  )
end

Instance Method Details

#serialize(strict = true) ⇒ Hash{Symbol => T.untyped}

Convert the log entry to a hash for serialization

Parameters:

  • strict (Boolean) (defaults to: true)

Returns:

  • (Hash{Symbol => T.untyped})


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/log_struct/log/error.rb', line 43

def serialize(strict = true)
  hash = serialize_common(strict)
  merge_additional_data_fields(hash)

  # Add exception-specific fields
  hash[LOG_KEYS.fetch(:err_class)] = err_class.name
  hash[LOG_KEYS.fetch(:message)] = message
  if backtrace.is_a?(Array) && backtrace&.any?
    hash[LOG_KEYS.fetch(:backtrace)] = backtrace&.first(10)
  end

  hash
end