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.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/betterlog/log/event.rb', line 40

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

Class Method Details

.ify(arg, **rest) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/betterlog/log/event.rb', line 6

def self.ify(arg, **rest)
  rest = rest.symbolize_keys_recursive(circular: :circular)
  if e = arg.ask_and_send(:exception)
    new(
      {
        error_class:  e.class.name,
        message:      "#{e.class.name}: #{e.message}",
        backtrace:    e.backtrace,
      } | rest
    )
  elsif message = arg.ask_and_send(:to_str)
    new({ message: } | rest)
  elsif hash = arg.ask_and_send(:to_hash)
    new(hash | rest)
  else
    message = "Logging #{arg.inspect}"
    new({ message: } | rest)
  end
end

.is?(json) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/betterlog/log/event.rb', line 31

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



26
27
28
29
# File 'lib/betterlog/log/event.rb', line 26

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

Instance Method Details

#[](name) ⇒ Object



80
81
82
# File 'lib/betterlog/log/event.rb', line 80

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

#[]=(name, value) ⇒ Object



76
77
78
# File 'lib/betterlog/log/event.rb', line 76

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

#as_json(*a) ⇒ Object



54
55
56
# File 'lib/betterlog/log/event.rb', line 54

def as_json(*a)
  @data.dup
end

#emitterObject



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

def emitter
  @data[:emitter]
end

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

Returns:

  • (Boolean)


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

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

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



70
71
72
# File 'lib/betterlog/log/event.rb', line 70

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

#hashObject



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

def hash
  @data.hash
end

#notify?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/betterlog/log/event.rb', line 92

def notify?
  @data[:notify]
end

#severityObject



84
85
86
# File 'lib/betterlog/log/event.rb', line 84

def severity
  @data[:severity]
end

#to_json(*a) ⇒ Object



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

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