Skip to content
This repository was archived by the owner on Dec 8, 2020. It is now read-only.

Commit a8ebe13

Browse files
committed
Re-add CurrentContext.with
1 parent d104e81 commit a8ebe13

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

lib/timber/current_context.rb

+40
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def remove(*args)
3838
def reset(*args)
3939
instance.reset(*args)
4040
end
41+
42+
# Convenience method for {CurrentContext#with}. See {CurrentContext#with} for more info.
43+
def with(*args, &block)
44+
instance.with(*args, &block)
45+
end
4146
end
4247

4348
# Adds contexts but does not remove them. See {#with} for automatic maintenance and {#remove}
@@ -68,6 +73,12 @@ def remove(*keys)
6873
self
6974
end
7075

76+
def replace(hash)
77+
@hash = hash
78+
expire_cache!
79+
self
80+
end
81+
7182
# Resets the context to be blank. Use this carefully! This will remove *any* context,
7283
# include context that is automatically included with Timber.
7384
def reset
@@ -83,6 +94,35 @@ def snapshot
8394
@snapshot ||= hash.clone
8495
end
8596

97+
# Adds a context and then removes it when the block is finished executing.
98+
#
99+
# @note Because context is included with every log line, it is recommended that you limit this
100+
# to only neccessary data.
101+
#
102+
# @example Adding a custom context
103+
# Timber::CurrentContext.with({build: {version: "1.0.0"}}) do
104+
# # ... anything logged here will include the context ...
105+
# end
106+
#
107+
# @note Any custom context needs to have a single root key to be valid. i.e. instead of:
108+
# Timber::CurrentContext.with(job_id: "123", job_name: "Refresh User Account")
109+
#
110+
# do
111+
#
112+
# Timber::CurrentContext.with(job: {job_id: "123", job_name: "Refresh User Account"})
113+
#
114+
# @example Adding multiple contexts
115+
# Timber::CurrentContext.with(context1, context2) { ... }
116+
def with(*objects)
117+
old_hash = hash.clone
118+
begin
119+
add(*objects)
120+
yield
121+
ensure
122+
replace(old_hash)
123+
end
124+
end
125+
86126
private
87127
# The internal hash that is maintained. Use {#with} and {#add} for hash maintenance.
88128
def hash

spec/timber/current_context_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,25 @@
8989
expect(described_class.instance.send(:hash)[:build]).to be_nil
9090
end
9191
end
92+
93+
describe ".with" do
94+
it "should merge the context and cleanup on block exit" do
95+
expect(described_class.instance.send(:hash)[:build]).to be_nil
96+
97+
described_class.with({build: {version: "1.0.0"}}) do
98+
expect(described_class.instance.send(:hash)[:build]).to eq({:version=>"1.0.0"})
99+
100+
described_class.with({testing: {key: "value"}}) do
101+
expect(described_class.instance.send(:hash)[:build]).to eq({:version=>"1.0.0"})
102+
expect(described_class.instance.send(:hash)[:testing]).to eq({:key=>"value"})
103+
end
104+
105+
expect(described_class.instance.send(:hash)[:build]).to eq({:version=>"1.0.0"})
106+
expect(described_class.instance.send(:hash)[:testing]).to be_nil
107+
end
108+
109+
expect(described_class.instance.send(:hash)[:build]).to be_nil
110+
expect(described_class.instance.send(:hash)[:testing]).to be_nil
111+
end
112+
end
92113
end

0 commit comments

Comments
 (0)