Class: Betterlog::Log::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/betterlog/log/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Event

Returns a new instance of Event.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/betterlog/log/event.rb', line 59

def initialize(data = {})
  data = data.symbolize_keys_recursive(circular: :circular) | meta
  unless data.key?(:message)
    data[:message] = "a #{data[:type]} type log message of severity #{data[:severity]}"
  end
  data[:severity] =
    begin
      Severity.new((data[:severity] || :debug))
    rescue
      Severity.new(:debug)
    end
  @data = Hash[data.sort_by(&:first)]
end

Class Method Details

.ify(arg, severity: :debug, notify: nil, rest: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/betterlog/log/event.rb', line 6

def self.ify(arg, severity: :debug, notify: nil, rest: {})
  notify ||= rest.delete(:notify)
  if e = arg.ask_and_send(:exception)
    ify(
      {
        error_class:  e.class.name,
        message:      "#{e.class.name}: #{e.message}",
        backtrace:    e.backtrace,
      },
      severity: severity,
      rest: rest,
      notify: notify,
    )
  elsif s = arg.ask_and_send(:to_str)
    new(
      ({ notify: s } if notify).to_h |
      {
        message:  s,
        severity: severity,
      } | rest
    )
  elsif h = arg.ask_and_send(:to_hash)
    arg = h | { severity: severity } | rest
    new(
      ({ notify: h[:message] || arg.to_s } if notify).to_h |
      arg
    )
  else
    message = "Logging #{arg.inspect}"
    new(
      ({ notify: message } if notify).to_h |
      {
        message: message,
        severity: severity,
      } | rest
    )
  end
end

.is?(json) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/betterlog/log/event.rb', line 50

def self.is?(json)
  if json = json.ask_and_send(:to_str)
    data = JSON.parse(json).ask_and_send(:to_hash)
    data&.key?('emitter')
  end
rescue JSON::ParserError
  false
end

.parse(json) ⇒ Object



45
46
47
48
# File 'lib/betterlog/log/event.rb', line 45

def self.parse(json)
  new(JSON.parse(json))
rescue JSON::ParserError
end

Instance Method Details

#[](name) ⇒ Object



99
100
101
# File 'lib/betterlog/log/event.rb', line 99

def [](name)
  @data[name.to_sym]
end

#[]=(name, value) ⇒ Object



95
96
97
# File 'lib/betterlog/log/event.rb', line 95

def []=(name, value)
  @data[name.to_sym] = value
end

#as_json(*a) ⇒ Object



73
74
75
# File 'lib/betterlog/log/event.rb', line 73

def as_json(*a)
  @data.dup
end

#emitterObject



107
108
109
# File 'lib/betterlog/log/event.rb', line 107

def emitter
  @data[:emitter]
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


115
116
117
# File 'lib/betterlog/log/event.rb', line 115

def eql?(other)
  @data.eql? other.instance_variable_get(:@data)
end

#format(**args) ⇒ Object Also known as: to_s



89
90
91
# File 'lib/betterlog/log/event.rb', line 89

def format(**args)
  Log::EventFormatter.new(self).format(**args)
end

#hashObject



121
122
123
# File 'lib/betterlog/log/event.rb', line 121

def hash
  @data.hash
end

#notify?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/betterlog/log/event.rb', line 111

def notify?
  @data[:notify]
end

#severityObject



103
104
105
# File 'lib/betterlog/log/event.rb', line 103

def severity
  @data[:severity]
end

#to_json(*a) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/betterlog/log/event.rb', line 77

def to_json(*a)
  JSON.generate(as_json)
rescue
  # Sometimes rails logging messages contain invalid utf-8 characters
  # generating various standard errors. Let's fallback to a barebones
  # event with just a cleaned up message for these cases.
  JSON.generate({
    severity: @data[:severity],
    message: @data.fetch(:message, '').encode('utf-8', invalid: :replace, undef: :replace, replace: ''),
  })
end