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.

Parameters:

  • parent_context (Context) (defaults to: nil)

    A parent context to inherit tags from.



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

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.

Parameters:

  • key (String, Symbol)

    The tag key.

Returns:

  • (Object)

    The tag value.



28
29
30
# File 'lib/lumberjack/context.rb', line 28

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

#[]=(key, value) ⇒ void

This method returns an undefined value.

Set a context tag.

Parameters:

  • key (String, Symbol)

    The tag key.

  • value (Object)

    The tag value.



37
38
39
# File 'lib/lumberjack/context.rb', line 37

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

#resetvoid

This method returns an undefined value.

Clear all the context data.



44
45
46
# File 'lib/lumberjack/context.rb', line 44

def reset
  @tags.clear
end

#tag(tags) ⇒ void

This method returns an undefined value.

Set tags on the context.

Parameters:

  • tags (Hash)

    The tags to set.



18
19
20
21
22
# File 'lib/lumberjack/context.rb', line 18

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