Class: Timber::Contexts::Runtime

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

Overview

The runtime context adds current runtime data to your logs, such as the file, line number, class or module name, etc. This makes it easy to tail and search your logs by their origin in your code. For example, if you are debugging a specific class, you can narrow by that class and see only it’s logs.

Constant Summary collapse

APPLICATION_MAX_BYTES =
256.freeze
CLASS_NAME_MAX_BYTES =
256.freeze
FILE_MAX_BYTES =
1024.freeze
FUNCTION_MAX_BYTES =
256.freeze
MODULE_NAME_MAX_BYTES =
256.freeze
VM_PID_MAX_BYTES =
256.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Runtime

Returns a new instance of Runtime.



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

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @application = normalizer.fetch(:application, :string, :limit => APPLICATION_MAX_BYTES)
  @class_name = normalizer.fetch(:class_name, :string, :limit => CLASS_NAME_MAX_BYTES)
  @file = normalizer.fetch(:file, :string, :limit => FILE_MAX_BYTES)
  @function = normalizer.fetch(:function, :string, :limit => FUNCTION_MAX_BYTES)
  @line = normalizer.fetch(:line, :integer)
  @module_name = normalizer.fetch(:module_name, :string, :limit => MODULE_NAME_MAX_BYTES)
  @vm_pid = normalizer.fetch(:vm_pid, :string, :limit => VM_PID_MAX_BYTES)
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def application
  @application
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def class_name
  @class_name
end

#fileObject (readonly)

Returns the value of attribute file.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def file
  @file
end

#functionObject (readonly)

Returns the value of attribute function.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def function
  @function
end

#lineObject (readonly)

Returns the value of attribute line.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def line
  @line
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def module_name
  @module_name
end

#vm_pidObject (readonly)

Returns the value of attribute vm_pid.



19
20
21
# File 'lib/timber/contexts/runtime.rb', line 19

def vm_pid
  @vm_pid
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



45
46
47
# File 'lib/timber/contexts/runtime.rb', line 45

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
42
43
# File 'lib/timber/contexts/runtime.rb', line 33

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:application, application)
    h.add(:class_name, class_name)
    h.add(:file, file)
    h.add(:function, function)
    h.add(:line, line)
    h.add(:module_name, module_name)
    h.add(:vm_pid, vm_pid)
  end
end