Class: Lumberjack::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/context.rb

Overview

A context is used to store tags that are then added to all log entries within a block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_context = nil) ⇒ Context

Returns a new instance of Context.



8
9
10
11
# File 'lib/lumberjack/context.rb', line 8

def initialize(parent_context = nil)
  @tags = {}
  @tags.merge!(parent_context.tags) if parent_context
end

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/lumberjack/context.rb', line 6

def tags
  @tags
end

Instance Method Details

#[](key) ⇒ Object

Get a context tag.



21
22
23
# File 'lib/lumberjack/context.rb', line 21

def [](key)
  @tags[key.to_s]
end

#[]=(key, value) ⇒ Object

Set a context tag.



26
27
28
# File 'lib/lumberjack/context.rb', line 26

def []=(key, value)
  @tags[key.to_s] = value
end

#resetObject

Clear all the context data.



31
32
33
# File 'lib/lumberjack/context.rb', line 31

def reset
  @tags.clear
end

#tag(tags) ⇒ Object

Set tags on the context.



14
15
16
17
18
# File 'lib/lumberjack/context.rb', line 14

def tag(tags)
  tags.each do |key, value|
    @tags[key.to_s] = value
  end
end