Class: Conscriptor::HierarchicalContextLogger
- Inherits:
-
Object
- Object
- Conscriptor::HierarchicalContextLogger
- Defined in:
- lib/conscriptor/hierarchical_context_logger.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
writeonly
Sets the attribute context.
Instance Method Summary collapse
-
#info(message) ⇒ Object
write a message with the context printed as well.
-
#initialize ⇒ HierarchicalContextLogger
constructor
A new instance of HierarchicalContextLogger.
-
#pop_context ⇒ Object
ideally, use #with_context instead.
- #pop_context_to(level:) ⇒ Object
-
#push_context(thing) ⇒ Object
ideally, use #with_context instead adds a level of context, e.g.
- #with_context(thing) ⇒ Object
Constructor Details
#initialize ⇒ HierarchicalContextLogger
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
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() @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 , @context.length end |
#pop_context ⇒ Object
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 |