Class: Timber::Contexts::Custom

Inherits:
Timber::Context
  • Object
show all
Defined in:
lib/timber/contexts/custom.rb

Overview

Custom contexts allow you to add application specific context not covered elsewhere.

Examples:

Adding a context

custom_context = Timber::Contexts::Custom.new(type: :keyspace, data: %{my: "data"})
Timber::CurrentContext.with(custom_context) do
  # ... anything logged here will have the context ...
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Custom

Returns a new instance of Custom.



15
16
17
18
# File 'lib/timber/contexts/custom.rb', line 15

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/timber/contexts/custom.rb', line 13

def data
  @data
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/timber/contexts/custom.rb', line 13

def type
  @type
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



20
21
22
# File 'lib/timber/contexts/custom.rb', line 20

def as_json(_options = {})
  {Timber::Util::Object.try(type, :to_sym) => data}
end