Class: Timber::CurrentContext
- Inherits:
-
Object
- Object
- Timber::CurrentContext
- Includes:
- Singleton
- Defined in:
- lib/timber/current_context.rb
Overview
Because context is appended to every log line, it is recommended that you limit this to only neccessary data needed to relate your log lines.
Holds the current context in a thread safe memory storage. This context is appended to every log line. Think of context as join data between your log lines, allowing you to relate them and filter them appropriately.
Constant Summary collapse
- THREAD_NAMESPACE =
:_timber_current_context.freeze
Class Method Summary collapse
-
.add(*args) ⇒ Object
Convenience method for #add.
- .hash(*args) ⇒ Object
-
.remove(*args) ⇒ Object
Convenience method for #remove.
-
.with(*args, &block) ⇒ Object
Convenience method for #with.
Instance Method Summary collapse
-
#add(*contexts) ⇒ Object
Adds contexts but does not remove them.
-
#hash ⇒ Object
The internal hash that is maintained.
-
#remove(*contexts) ⇒ Object
Removes a context.
-
#snapshot ⇒ Object
Snapshots the current context so that you get a moment in time representation of the context, since the context can change as execution proceeds.
-
#with(*contexts) ⇒ Object
Adds a context and then removes it when the block is finished executing.
Class Method Details
.add(*args) ⇒ Object
22 23 24 |
# File 'lib/timber/current_context.rb', line 22 def add(*args) instance.add(*args) end |
.hash(*args) ⇒ Object
31 32 33 |
# File 'lib/timber/current_context.rb', line 31 def hash(*args) instance.hash(*args) end |
.remove(*args) ⇒ Object
27 28 29 |
# File 'lib/timber/current_context.rb', line 27 def remove(*args) instance.remove(*args) end |
Instance Method Details
#add(*contexts) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/timber/current_context.rb', line 70 def add(*contexts) contexts.each do |context| key = context.keyspace json = context.as_json # Convert to json now so that we aren't doing it for every line if key == :custom # Custom contexts are merged into the space hash[key] ||= {} hash[key].merge(json) else hash[key] = json end end end |
#hash ⇒ Object
94 95 96 |
# File 'lib/timber/current_context.rb', line 94 def hash Thread.current[THREAD_NAMESPACE] ||= {} end |
#remove(*contexts) ⇒ Object
Removes a context. This must be a Timber::Context type. See Timber::Contexts for a list. If you wish to remove by key, or some other way, use #hash and modify the hash accordingly.
86 87 88 89 90 |
# File 'lib/timber/current_context.rb', line 86 def remove(*contexts) contexts.each do |context| hash.delete(context.keyspace) end end |
#snapshot ⇒ Object
Snapshots the current context so that you get a moment in time representation of the context, since the context can change as execution proceeds.
100 101 102 |
# File 'lib/timber/current_context.rb', line 100 def snapshot hash.clone end |
#with(*contexts) ⇒ Object
Because context is included with every log line, it is recommended that you limit this to only neccessary data.
Adds a context and then removes it when the block is finished executing.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/timber/current_context.rb', line 51 def with(*contexts) add(*contexts) yield ensure contexts.each do |context| if context.keyspace == :custom # Custom contexts are merged and should be removed the same hash[context.keyspace].delete(context.type) else remove(context) end end end |