Class: Betterlog::Log::Event
- Inherits:
-
Object
- Object
- Betterlog::Log::Event
- Defined in:
- lib/betterlog/log/event.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #as_json(*a) ⇒ Object
- #emitter ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #format(**args) ⇒ Object (also: #to_s)
- #hash ⇒ Object
-
#initialize(data = {}) ⇒ Event
constructor
A new instance of Event.
- #notify? ⇒ Boolean
- #severity ⇒ Object
- #to_json(*a) ⇒ Object
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 = arg.ask_and_send(:to_str) new({ message: } | rest) elsif hash = arg.ask_and_send(:to_hash) new(hash | rest) else = "Logging #{arg.inspect}" new({ message: } | rest) end end |
.is?(json) ⇒ 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 |
#emitter ⇒ Object
88 89 90 |
# File 'lib/betterlog/log/event.rb', line 88 def emitter @data[:emitter] end |
#eql?(other) ⇒ Boolean Also known as: ==
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 |
#hash ⇒ Object
102 103 104 |
# File 'lib/betterlog/log/event.rb', line 102 def hash @data.hash end |
#notify? ⇒ Boolean
92 93 94 |
# File 'lib/betterlog/log/event.rb', line 92 def notify? @data[:notify] end |
#severity ⇒ Object
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 |