Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log entry metadata when it's not empty #45

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/json_log_formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe Dexter::JSONLogFormatter do
)
end

it "formats the entry metadata as json" do
io = IO::Memory.new
entry = build_entry({my_data: "is great!"}, source: "json-test", severity: :debug, data: Log::Metadata.build({metadata: "is great!", more_data: "more!"}))
format(entry, io)
io.to_s.chomp.should eq(
{severity: "Debug", source: "json-test", timestamp: timestamp, data: {metadata: "is great!", more_data: "more!"}, my_data: "is great!"}.to_json
)
end

it "formats complex types" do
io = IO::Memory.new
entry = build_entry({args: [1], params: {foo: "bar"}, other: {arr: [1]}}, source: "json-test", severity: :debug)
Expand Down
18 changes: 13 additions & 5 deletions src/dexter/json_log_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ module Dexter
end

private def default_data
data = {
"severity" => entry.severity.to_s,
"source" => entry.source,
"timestamp" => entry.timestamp,
}
data = Hash(String, String | Time | Hash(String, String)).new
data["severity"] = entry.severity.to_s
data["source"] = entry.source
data["timestamp"] = entry.timestamp
data["data"] = metadata unless entry.data.empty?
data["message"] = entry.message unless entry.message.empty?
data
end

private def metadata
Hash(String, String).new.tap do |hash|
entry.data.each do |key, value|
hash[key.to_s] = value.to_s
end
end
end

private def exception_data
entry.exception.try do |ex|
{"error" => {"class" => ex.class.name, "message" => ex.message, "backtrace" => ex.backtrace?}}
Expand Down