Skip to content

Commit

Permalink
Append context in stdio backend.
Browse files Browse the repository at this point in the history
Cleanup context on specs that changes it
  • Loading branch information
bcardiff committed Feb 25, 2020
1 parent 5f4d765 commit 90e2340
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/std/log/context_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ describe Log::Context do
Log.context.set a: 1
Log.context.set b: 2
Log.context.should eq(c({a: 1, b: 2}))
Log.context.clear

This comment has been minimized.

Copy link
@Sija

Sija Feb 25, 2020

Contributor

around_each? ditto elsewhere

This comment has been minimized.

Copy link
@asterite

asterite Feb 25, 2020

Member

Even though we have it, I'd prefer to avoid using in the standard library specs. In my opinion hooks always make specs harder to read. You have to scroll up and down to understand them.

This comment has been minimized.

Copy link
@straight-shoota

straight-shoota Feb 25, 2020

Member

Please avoid pointing out such nitty details that could be improved at any time. We're still in the process of figuring out the grand scheme of Log implementation.

This comment has been minimized.

Copy link
@Sija

Sija Feb 25, 2020

Contributor

@straight-shoota I understand that, but this has nothing to do with it. Whether hooks are being used in testing is a completely different matter to the whole "grand scheme", so I don't see your point here.

This comment has been minimized.

Copy link
@straight-shoota

straight-shoota Feb 25, 2020

Member

The point is that such comments distract from what's important to discuss right now.

This comment has been minimized.

Copy link
@Sija

Sija Feb 25, 2020

Contributor

I thought that's what's going on in #5874, this PR is about the concrete impl., not the grand scheme anymoar.

end

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

it "is restored with using" do
Expand All @@ -64,6 +66,7 @@ describe Log::Context do
end

Log.context.should eq(c({a: 1}))
Log.context.clear
end

it "is per fiber" do
Expand All @@ -83,6 +86,7 @@ describe Log::Context do
done.send nil
Log.context.should eq(c({a: 1}))
done.send nil
Log.context.clear
end
end
end
2 changes: 2 additions & 0 deletions spec/std/log/log_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ describe Log do
log.info { "info message" }

backend.entries.first.context.should eq(Log::Context.new({a: 1}))

Log.context.clear
end
end
13 changes: 13 additions & 0 deletions spec/std/log/stdio_backend_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ describe Log::StdioBackend do
end
end

it "logs context" do
IO.pipe do |r, w|
logger = stdio_logger(stdout: w)
Log.context.clear
Log.context.using do
Log.context.set foo: "foo"
logger.info { "info:show" }
end

r.gets.should match(/info:show -- {:foo => "foo"}/)
end
end

it "logs any object" do
IO.pipe do |r, w|
logger = stdio_logger(stdout: w)
Expand Down
3 changes: 3 additions & 0 deletions src/log/stdio_backend.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Log::StdioBackend < Log::Backend
label = entry.severity.to_s.upcase
io << label[0] << ", [" << entry.timestamp << " #" << Process.pid << "] "
io << label.rjust(7) << " -- " << @progname << ":" << entry.source << ": " << entry.message
if entry.context.size > 0
io << " -- " << entry.context
end
if ex = entry.exception
io << " -- " << ex.class << ": " << ex
end
Expand Down

0 comments on commit 90e2340

Please sign in to comment.