Class: Timber::Contexts::User

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

Overview

Note:

This is tracked automatically with the Integrations::Rack::UserContext rack middleware for supported authentication frameworks. See Integrations::Rack::UserContext for more details.

The user context adds data about the currently authenticated user to your logs. By adding this context all of your logs will contain user information. This allows filter and tail logs by specific users.

Constant Summary collapse

ID_MAX_BYTES =
256.freeze
NAME_MAX_BYTES =
256.freeze
EMAIL_MAX_BYTES =
256.freeze
TYPE_MAX_BYTES =
256.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ User

Returns a new instance of User.



23
24
25
26
27
28
29
30
# File 'lib/timber/contexts/user.rb', line 23

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @id = normalizer.fetch(:id, :string, :limit => ID_MAX_BYTES)
  @name = normalizer.fetch(:name, :string, :limit => NAME_MAX_BYTES)
  @email = normalizer.fetch(:email, :string, :limit => EMAIL_MAX_BYTES)
  @type = normalizer.fetch(:type, :string, :limit => TYPE_MAX_BYTES)
  @meta = normalizer.fetch(:meta, :hash)
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



21
22
23
# File 'lib/timber/contexts/user.rb', line 21

def email
  @email
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/timber/contexts/user.rb', line 21

def id
  @id
end

#metaObject (readonly)

Returns the value of attribute meta.



21
22
23
# File 'lib/timber/contexts/user.rb', line 21

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/timber/contexts/user.rb', line 21

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/timber/contexts/user.rb', line 21

def type
  @type
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



43
44
45
# File 'lib/timber/contexts/user.rb', line 43

def as_json(_options = {})
  to_hash
end

#to_hashObject

Builds a hash representation containing simple objects, suitable for serialization (JSON).



33
34
35
36
37
38
39
40
41
# File 'lib/timber/contexts/user.rb', line 33

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:id, id)
    h.add(:name, name)
    h.add(:email, email)
    h.add(:type, type)
    h.add(:meta, meta)
  end
end