Sha256: 4960c45ad2f6a5e57571e369a08661ddbd616ed14100b8d653f0a5a12a33d6c7

Contents?: true

Size: 1.46 KB

Versions: 16

Compression:

Stored size: 1.46 KB

Contents

module Timber
  module Events
    # 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}.
    class Custom < Timber::Event
      attr_reader :type, :message, :data

      # Instantiates a new custom event that can be logged. See {Logger} for examples
      # on logging custom events.
      #
      # @param [Hash] attributes the options to create a custom event with.
      # @option attributes [Symbol] :type *required* The custom event type. This should be in
      #   snake case. Example: `:my_custom_event`.
      # @option attributes [String] :message *required* The message to be logged.
      # @option attributes [Hash] :data A hash of JSON encodable data to be stored with the
      #   log line.
      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

      def to_hash
        {type => data}
      end
      alias to_h to_hash

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

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

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
timber-1.1.2 lib/timber/events/custom.rb
timber-1.0.13 lib/timber/events/custom.rb
timber-1.0.12 lib/timber/events/custom.rb
timber-1.0.11 lib/timber/events/custom.rb
timber-1.0.10 lib/timber/events/custom.rb
timber-1.0.9 lib/timber/events/custom.rb
timber-1.0.8 lib/timber/events/custom.rb
timber-1.0.7 lib/timber/events/custom.rb
timber-1.0.6 lib/timber/events/custom.rb
timber-1.0.5 lib/timber/events/custom.rb
timber-1.0.4 lib/timber/events/custom.rb
timber-1.0.3 lib/timber/events/custom.rb
timberio-1.0.3 lib/timber/events/custom.rb
timberio-1.0.2 lib/timber/events/custom.rb
timberio-1.0.1 lib/timber/events/custom.rb
timberio-1.0.0 lib/timber/events/custom.rb