Skip to content

Commit

Permalink
Add Log.with_context with kwargs (#11517)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspiano authored Dec 10, 2021
1 parent bdc3e6e commit 903ebe6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 60 deletions.
158 changes: 104 additions & 54 deletions spec/std/log/context_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private def m(value)
Log::Metadata.build(value)
end

describe "Log.context" do
describe Log do
before_each do
Log.context.clear
end
Expand All @@ -14,80 +14,130 @@ describe "Log.context" do
Log.context.clear
end

it "can be set and cleared" do
Log.context.metadata.should eq(Log::Metadata.new)
describe ".context" do
it "can be set and cleared" do
Log.context.metadata.should eq(Log::Metadata.new)

Log.context.set a: 1
Log.context.metadata.should eq(m({a: 1}))
Log.context.set a: 1
Log.context.metadata.should eq(m({a: 1}))

Log.context.clear
Log.context.metadata.should eq(Log::Metadata.new)
end
Log.context.clear
Log.context.metadata.should eq(Log::Metadata.new)
end

it "is extended by set" do
Log.context.set a: 1
Log.context.set b: 2
Log.context.metadata.should eq(m({a: 1, b: 2}))
end
it "is extended by set" do
Log.context.set a: 1
Log.context.set b: 2
Log.context.metadata.should eq(m({a: 1, b: 2}))
end

it "existing keys are overwritten by set" do
Log.context.set a: 1, b: 1
Log.context.set b: 2, c: 3
Log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end
it "existing keys are overwritten by set" do
Log.context.set a: 1, b: 1
Log.context.set b: 2, c: 3
Log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end

it "is restored after with_context" do
Log.context.set a: 1
it "is per fiber" do
Log.context.set a: 1
done = Channel(Nil).new

Log.with_context do
Log.context.set b: 2
f = spawn do
Log.context.metadata.should eq(Log::Metadata.new)
Log.context.set b: 2
Log.context.metadata.should eq(m({b: 2}))

done.receive
done.receive
end

done.send nil
Log.context.metadata.should eq(m({a: 1}))
done.send nil
end

it "is assignable from a hash with symbol keys" do
Log.context.set a: 1
extra = {:b => 2}
Log.context.set extra
Log.context.metadata.should eq(m({a: 1, b: 2}))
end

Log.context.metadata.should eq(m({a: 1}))
it "is assignable from a named tuple" do
Log.context.set a: 1
extra = {b: 2}
Log.context.set extra
Log.context.metadata.should eq(m({a: 1, b: 2}))
end
end

it "is restored after with_context of Log instance" do
Log.context.set a: 1
log = Log.for("temp")
describe "#with_context" do
it "with arguments restores context after the block" do
Log.context.set a: 1
log = Log.for("temp")

log.with_context do
log.context.set b: 2
log.context.metadata.should eq(m({a: 1, b: 2}))
log.with_context(b: 2) do
log.context.set c: 3
log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end

log.context.metadata.should eq(m({a: 1}))
end

log.context.metadata.should eq(m({a: 1}))
it "restores context after the block" do
Log.context.set a: 1
log = Log.for("temp")

log.with_context do
log.context.set b: 2
log.context.metadata.should eq(m({a: 1, b: 2}))
end

log.context.metadata.should eq(m({a: 1}))
end
end

it "is per fiber" do
Log.context.set a: 1
done = Channel(Nil).new
describe ".with_context" do
it "with arguments restores context after the block" do
Log.context.set a: 1
Log.with_context(b: 2) do
Log.context.set c: 3
Log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end

f = spawn do
Log.context.metadata.should eq(Log::Metadata.new)
Log.context.set b: 2
Log.context.metadata.should eq(m({b: 2}))
Log.context.metadata.should eq(m({a: 1}))
end

it "restores context after the block" do
Log.context.set a: 1

done.receive
done.receive
Log.with_context do
Log.context.set b: 2
Log.context.metadata.should eq(m({a: 1, b: 2}))
end

Log.context.metadata.should eq(m({a: 1}))
end

done.send nil
Log.context.metadata.should eq(m({a: 1}))
done.send nil
end
it "assigns context via a hash with symbol keys" do
Log.context.set a: 1
extra = {:b => 2}
Log.with_context(extra) do
Log.context.set c: 3
Log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end

it "is assignable from a hash with symbol keys" do
Log.context.set a: 1
extra = {:b => 2}
Log.context.set extra
Log.context.metadata.should eq(m({a: 1, b: 2}))
end
Log.context.metadata.should eq(m({a: 1}))
end

it "is assignable from a named tuple" do
Log.context.set a: 1
extra = {b: 2}
Log.context.set extra
Log.context.metadata.should eq(m({a: 1, b: 2}))
it "assigns context via a named tuple" do
Log.context.set a: 1
extra = {b: 2}
Log.with_context(extra) do
Log.context.set c: 3
Log.context.metadata.should eq(m({a: 1, b: 2, c: 3}))
end

Log.context.metadata.should eq(m({a: 1}))
end
end
end
32 changes: 26 additions & 6 deletions src/log/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,20 @@ class Log
end

# Method to save and restore the current logging context.
# Temporary context for the duration of the block can be set via arguments.
#
# ```
# Log.context.set a: 1
# Log.info { %(message with {"a" => 1} context) }
# Log.with_context do
# Log.context.set b: 2
# Log.info { %(message with {"a" => 1, "b" => 2} context) }
# Log.with_context(b: 2) do
# Log.context.set c: 3
# Log.info { %(message with {"a" => 1, "b" => 2, "c" => 3} context) }
# end
# Log.info { %(message with {"a" => 1} context) }
# ```
def self.with_context
def self.with_context(**kwargs)
previous = Log.context
Log.context.set(**kwargs) unless kwargs.empty?
begin
yield
ensure
Expand All @@ -102,8 +104,26 @@ class Log
end

# :ditto:
def with_context
self.class.with_context do
def self.with_context(values)
previous = Log.context
Log.context.set(values) unless values.empty?
begin
yield
ensure
Log.context = previous
end
end

# :ditto:
def with_context(**kwargs)
self.class.with_context(**kwargs) do
yield
end
end

# :ditto:
def with_context(values)
self.class.with_context(values) do
yield
end
end
Expand Down

0 comments on commit 903ebe6

Please sign in to comment.