lib/timber/current_context.rb in timber-2.1.0.rc3 vs lib/timber/current_context.rb in timber-2.1.0.rc4
- old
+ new
@@ -1,7 +1,10 @@
+require "socket"
+
require "timber/config"
require "timber/contexts/release"
+require "timber/contexts/system"
module Timber
# 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.
@@ -79,11 +82,11 @@
#
# @note Because context is included with every log line, it is recommended that you limit this
# to only neccessary data.
def add(*objects)
objects.each do |object|
- add_to(hash, object)
+ add_to!(hash, object)
end
expire_cache!
self
end
@@ -140,17 +143,26 @@
# Builds the initial hash. This is extract into a method to support a threaded
# environment. Each thread holds it's own context and also needs to instantiate
# it's hash properly.
def build_initial_hash
new_hash = {}
+
+ # Release context
release_context = Contexts::Release.from_env
if release_context
- add_to(new_hash, release_context)
+ add_to!(new_hash, release_context)
end
+
+ # System context
+ hostname = Socket.gethostname
+ pid = Process.pid
+ system_context = Contexts::System.new(hostname: hostname, pid: pid)
+ add_to!(new_hash, system_context)
+
new_hash
end
- def add_to(hash, object)
+ def add_to!(hash, object)
context = Contexts.build(object) # Normalizes objects into a Timber::Context descendant.
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