Class: LogStruct::Log::Plain

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

Overview

Plain log entry for structured logging

Constant Summary collapse

PlainEvent =
T.type_alias {
  Event::Log
}

Instance Attribute 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: T.let(Source::App, Source), event: T.let(Event::Log, PlainEvent), level: T.let(Level::Info, Level), timestamp:, message:, additional_data: {}) ⇒ void

Parameters:

  • source (Source) (defaults to: T.let(Source::App, Source))

    Common fields

  • event (PlainEvent) (defaults to: T.let(Event::Log, PlainEvent))
  • level (Level) (defaults to: T.let(Level::Info, Level))
  • timestamp (Time)
  • message (T.untyped)

    Plain log messages can be any type (String, Number, Array, Hash, etc.) Developers might do something like Rails.logger.info(123) or Rails.logger.info(@variable) when debugging, or gems might send all kinds of random stuff to the logger. We don't want to crash with a type error in any of these cases.

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

    Allow people to submit additional data



# File ''

const :source, Source, default: T.let(Source::App, Source)
const :event, PlainEvent, default: T.let(Event::Log, PlainEvent)
const :level, Level, default: T.let(Level::Info, Level)
const :timestamp, Time, factory: -> { Time.now }
const :message, T.untyped
const :additional_data, T::Hash[Symbol, T.untyped], default: {}

Instance Attribute Details

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

Allow people to submit additional data

Returns:

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


# File ''

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

#eventPlainEvent (readonly)

Returns the value of prop event.

Returns:



# File ''

const :event, PlainEvent, default: T.let(Event::Log, PlainEvent)

#levelLevel (readonly)

Returns the value of prop level.

Returns:



# File ''

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

#messageT.untyped (readonly)

Plain log messages can be any type (String, Number, Array, Hash, etc.) Developers might do something like Rails.logger.info(123) or Rails.logger.info(@variable) when debugging, or gems might send all kinds of random stuff to the logger. We don't want to crash with a type error in any of these cases.

Returns:

  • (T.untyped)


# File ''

const :message, T.untyped

#sourceSource (readonly)

Common fields

Returns:



# File ''

const :source, Source, default: T.let(Source::App, Source)

#timestampTime (readonly)

Returns the value of prop timestamp.

Returns:

  • (Time)


# File ''

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

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})


45
46
47
48
49
50
# File 'lib/log_struct/log/plain.rb', line 45

def serialize(strict = true)
  hash = serialize_common(strict)
  merge_additional_data_fields(hash)
  hash[LOG_KEYS.fetch(:message)] = message
  hash
end