From b92a3f9a21a37b5cbc2e8a2d8baa938894790187 Mon Sep 17 00:00:00 2001 From: Chao Yang Date: Sun, 12 Dec 2021 05:11:58 +0000 Subject: [PATCH 1/2] Log entry metadata when it's not empty --- spec/json_log_formatter_spec.cr | 9 +++++++++ src/dexter/json_log_formatter.cr | 1 + 2 files changed, 10 insertions(+) diff --git a/spec/json_log_formatter_spec.cr b/spec/json_log_formatter_spec.cr index 1fd75ed..8583ced 100644 --- a/spec/json_log_formatter_spec.cr +++ b/spec/json_log_formatter_spec.cr @@ -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!"}.to_json, 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) diff --git a/src/dexter/json_log_formatter.cr b/src/dexter/json_log_formatter.cr index f2aa784..ffdb759 100644 --- a/src/dexter/json_log_formatter.cr +++ b/src/dexter/json_log_formatter.cr @@ -29,6 +29,7 @@ module Dexter "source" => entry.source, "timestamp" => entry.timestamp, } + data["data"] = entry.data.to_json unless entry.data.empty? data["message"] = entry.message unless entry.message.empty? data end From ff5350b7aa5e7150a4b52986994439698c16a10f Mon Sep 17 00:00:00 2001 From: Chao Yang Date: Sun, 12 Dec 2021 07:53:27 +0000 Subject: [PATCH 2/2] Convert metadata to hash --- spec/json_log_formatter_spec.cr | 2 +- src/dexter/json_log_formatter.cr | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/spec/json_log_formatter_spec.cr b/spec/json_log_formatter_spec.cr index 8583ced..d0a79ab 100644 --- a/spec/json_log_formatter_spec.cr +++ b/spec/json_log_formatter_spec.cr @@ -17,7 +17,7 @@ describe Dexter::JSONLogFormatter do 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!"}.to_json, my_data: "is great!"}.to_json + {severity: "Debug", source: "json-test", timestamp: timestamp, data: {metadata: "is great!", more_data: "more!"}, my_data: "is great!"}.to_json ) end diff --git a/src/dexter/json_log_formatter.cr b/src/dexter/json_log_formatter.cr index ffdb759..50e5c25 100644 --- a/src/dexter/json_log_formatter.cr +++ b/src/dexter/json_log_formatter.cr @@ -24,16 +24,23 @@ module Dexter end private def default_data - data = { - "severity" => entry.severity.to_s, - "source" => entry.source, - "timestamp" => entry.timestamp, - } - data["data"] = entry.data.to_json unless entry.data.empty? + 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?}}