-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_logger_examples_test.exs
37 lines (31 loc) · 1.26 KB
/
json_logger_examples_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule JSONLoggerExamplesTest do
use ExUnit.Case
import ExUnit.CaptureLog
require Logger
test "simple log" do
msg = "Hello, World!"
log_msg = capture_log(fn -> Logger.info(msg) end)
assert %{"msg" => ^msg, "level" => "info"} = Poison.decode!(log_msg)
end
test "log string list" do
msg = ["a", "b", "c"]
log_msg = capture_log(fn -> Logger.error(["a", "b", "c"]) end)
left_msg = List.to_string(msg)
assert %{"msg" => ^left_msg, "level" => "error"} = Poison.decode!(log_msg)
end
test "log with track_id metadata" do
Logger.configure_backend(:console, metadata: [:track_id])
Logger.metadata(track_id: "IAMATrackId")
msg = "I should have a track id attached at track_id"
log_msg = capture_log(fn -> Logger.debug(msg) end)
assert %{"msg" => ^msg, "level" => "debug", "track_id" => "IAMATrackId"} =
Poison.decode!(log_msg)
end
test "log with track_id metadata in another way" do
Logger.configure_backend(:console, metadata: [:track_id])
msg = "I should have a track id attached at track_id"
log_msg = capture_log(fn -> Logger.debug(msg, track_id: "IAMATrackId") end)
assert %{"msg" => ^msg, "level" => "debug", "track_id" => "IAMATrackId"} =
Poison.decode!(log_msg)
end
end