Class: Timber::Events::Custom

Inherits:
Timber::Event
  • Object
show all
Defined in:
lib/timber/events/custom.rb

Overview

Allows for custom events that aren’t covered elsewhere.

Custom events can be used to encode information about events that are central to your line of business like receiving credit card payments, saving a draft of a post, or changing a user’s password.

For examples of logging custom events see Logger.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Custom

Instantiates a new custom event that can be logged. See Logger for examples on logging custom events.

Parameters:

  • attributes (Hash)

    the options to create a custom event with.

Options Hash (attributes):

  • :type (Symbol)

    required The custom event type. This should be in snake case. Example: ‘:my_custom_event`.

  • :message (String)

    required The message to be logged.

  • :data (Hash)

    A hash of JSON encodable data to be stored with the log line.



22
23
24
25
26
# File 'lib/timber/events/custom.rb', line 22

def initialize(attributes)
  @type = attributes[:type] || raise(ArgumentError.new(":type is required"))
  @message = attributes[:message] || raise(ArgumentError.new(":message is required"))
  @data = attributes[:data]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/timber/events/custom.rb', line 11

def data
  @data
end

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/timber/events/custom.rb', line 11

def message
  @message
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/timber/events/custom.rb', line 11

def type
  @type
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



33
34
35
# File 'lib/timber/events/custom.rb', line 33

def as_json(_options = {})
  {:custom => to_hash}
end

#to_hashObject Also known as: to_h



28
29
30
# File 'lib/timber/events/custom.rb', line 28

def to_hash
  {type => data}
end

#to_json(options = {}) ⇒ Object



37
38
39
# File 'lib/timber/events/custom.rb', line 37

def to_json(options = {})
  as_json().to_json(options)
end