Class: LogStruct::Log::Error
- Inherits:
-
T::Struct
- Object
- T::Struct
- LogStruct::Log::Error
- 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
Instance Attribute Summary collapse
-
#additional_data ⇒ Hash{Symbol => T.untyped}
readonly
Returns the value of prop
additional_data
. -
#backtrace ⇒ Array<String>?
readonly
Returns the value of prop
backtrace
. -
#err_class ⇒ T.class_of(StandardError)
readonly
Exception-specific fields.
-
#event ⇒ ErrorEvent
readonly
Returns the value of prop
event
. -
#level ⇒ Level
readonly
Returns the value of prop
level
. -
#message ⇒ String
readonly
Returns the value of prop
message
. -
#source ⇒ Source
readonly
Common fields.
-
#timestamp ⇒ Time
readonly
Returns the value of prop
timestamp
.
Class Method Summary collapse
-
.from_exception(source, ex, additional_data = {}) ⇒ Log::Error
Create an Error log from a Ruby StandardError.
Instance Method Summary collapse
- #initialize(source:, event: T.let(Event::Error, ErrorEvent), timestamp:, level: T.let(Level::Error, Level), err_class:, message:, backtrace: nil, additional_data: {}) ⇒ void constructor
-
#serialize(strict = true) ⇒ Hash{Symbol => T.untyped}
Convert the log entry to a hash for serialization.
Methods included from MergeAdditionalDataFields
Methods included from SerializeCommon
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
|
# 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_data ⇒ Hash{Symbol => T.untyped} (readonly)
Returns the value of prop additional_data
.
|
# File '' const :additional_data, T::Hash[Symbol, T.untyped], default: {} |
#backtrace ⇒ Array<String>? (readonly)
Returns the value of prop backtrace
.
|
# File '' const :backtrace, T.nilable(T::Array[String]), default: nil |
#err_class ⇒ T.class_of(StandardError) (readonly)
Exception-specific fields
|
# File '' const :err_class, T.class_of(StandardError) |
#event ⇒ ErrorEvent (readonly)
Returns the value of prop event
.
|
# File '' const :event, ErrorEvent, default: T.let(Event::Error, ErrorEvent) |
#level ⇒ Level (readonly)
Returns the value of prop level
.
|
# File '' const :level, Level, default: T.let(Level::Error, Level) |
#message ⇒ String (readonly)
Returns the value of prop message
.
|
# File '' const :message, String |
#timestamp ⇒ Time (readonly)
Returns the value of prop timestamp
.
|
# 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
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., 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
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)] = if backtrace.is_a?(Array) && backtrace&.any? hash[LOG_KEYS.fetch(:backtrace)] = backtrace&.first(10) end hash end |