Class: Cogger::Entry

Inherits:
Data
  • Object
show all
Defined in:
lib/cogger/entry.rb

Overview

Defines a log entry which can be formatted for output.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: Program.call, level: "INFO", at: ::Time.now, message: nil, tags: Core::EMPTY_ARRAY, datetime_format: DATETIME_FORMAT, payload: Core::EMPTY_HASH) ⇒ Entry

Returns a new instance of Entry.



47
48
49
50
51
52
53
54
55
# File 'lib/cogger/entry.rb', line 47

def initialize id: Program.call,
               level: "INFO",
               at: ::Time.now,
               message: nil,
               tags: Core::EMPTY_ARRAY,
               datetime_format: DATETIME_FORMAT,
               payload: Core::EMPTY_HASH
  super
end

Instance Attribute Details

#atObject (readonly)

Returns the value of attribute at

Returns:

  • (Object)

    the current value of at



7
8
9
# File 'lib/cogger/entry.rb', line 7

def at
  @at
end

#datetime_formatObject (readonly)

Returns the value of attribute datetime_format

Returns:

  • (Object)

    the current value of datetime_format



7
8
9
# File 'lib/cogger/entry.rb', line 7

def datetime_format
  @datetime_format
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



7
8
9
# File 'lib/cogger/entry.rb', line 7

def id
  @id
end

#levelObject (readonly)

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



7
8
9
# File 'lib/cogger/entry.rb', line 7

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



7
8
9
# File 'lib/cogger/entry.rb', line 7

def message
  @message
end

#payloadObject (readonly)

Returns the value of attribute payload

Returns:

  • (Object)

    the current value of payload



7
8
9
# File 'lib/cogger/entry.rb', line 7

def payload
  @payload
end

#tagsObject (readonly)

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



7
8
9
# File 'lib/cogger/entry.rb', line 7

def tags
  @tags
end

Class Method Details

.for(message = nil, **payload) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cogger/entry.rb', line 8

def self.for(message = nil, **payload, &)
  content = block_given? ? yield : message

  new id: payload.delete(:id) || Program.call,
      level: (payload.delete(:level) || "INFO").upcase,
      at: payload.delete(:at) || ::Time.now,
      message: sanitize!(content, payload),
      tags: Array(payload.delete(:tags)),
      datetime_format: payload.delete(:datetime_format) || DATETIME_FORMAT,
      payload:
end

.for_crash(message, error, id:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/cogger/entry.rb', line 20

def self.for_crash message, error, id:
  new id:,
      level: "FATAL",
      message:,
      payload: {
        error_message: error.message,
        error_class: error.class,
        backtrace: error.backtrace
      }
end

Instance Method Details

#attributesObject



57
# File 'lib/cogger/entry.rb', line 57

def attributes = {id:, level:, at:, message:, **payload}

#tagged(tagger: Tag) ⇒ Object



67
68
69
70
71
72
# File 'lib/cogger/entry.rb', line 67

def tagged tagger: Tag
  attributes.tap do |pairs|
    computed_tags = tagger.for(*tags)
    pairs[:message] = "#{computed_tags} #{pairs[:message]}" unless computed_tags.empty?
  end
end

#tagged_attributes(tagger: Tag) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/cogger/entry.rb', line 59

def tagged_attributes tagger: Tag
  computed_tags = tagger.for(*tags)

  return attributes if computed_tags.empty?

  {id:, level:, at:, message:, **computed_tags.to_h, **payload}
end