Class: Conscriptor::HierarchicalContextLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/conscriptor/hierarchical_context_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHierarchicalContextLogger

Returns a new instance of HierarchicalContextLogger.



12
13
14
15
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 12

def initialize
  @context = []
  @printed_context = []
end

Instance Attribute Details

#context=(value) ⇒ Object (writeonly)

Sets the attribute context

Parameters:

  • value

    the value to set the attribute context to.



10
11
12
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 10

def context=(value)
  @context = value
end

Instance Method Details

#info(message) ⇒ Object

write a message with the context printed as well



18
19
20
21
22
23
24
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 18

def info(message)
  @context.length.times do |level|
    puts_at_level @context[level], level if @context[0..level] != @printed_context[0..level]
  end
  @printed_context = @context.dup
  puts_at_level message, @context.length
end

#pop_contextObject

ideally, use #with_context instead



40
41
42
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 40

def pop_context
  @context.pop
end

#pop_context_to(level:) ⇒ Object



44
45
46
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 44

def pop_context_to(level:)
  @context.pop while @context.length > level
end

#push_context(thing) ⇒ Object

ideally, use #with_context instead adds a level of context, e.g. ‘school’ or ‘user’



35
36
37
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 35

def push_context(thing)
  @context.push(thing)
end

#with_context(thing) ⇒ Object



26
27
28
29
30
31
# File 'lib/conscriptor/hierarchical_context_logger.rb', line 26

def with_context(thing)
  push_context(thing)
  yield
ensure
  pop_context
end