Skip to content

Commit

Permalink
Fixes tagged logging (#185)
Browse files Browse the repository at this point in the history
Mainly just restores the JSON formatter to its old `LogChooser`
algorithm:


c5f52dc93#diff-b5c693951741188b864854a97fd5cdc4eaa20c335ae29492f2c36d7877bb4965L29-L36
  • Loading branch information
neildecapia authored Sep 26, 2023
1 parent d4d1d0c commit 9bf3e54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/lumberaxe/lib/lumberaxe/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class JSONFormatter < ::Logger::Formatter
include ActiveSupport::TaggedLogging::Formatter

def call(severity, time, progname, data)
data = data.is_a?(Hash) ? format_data(data) : { message: data.to_s }
data = { message: data.to_s } unless data.is_a?(Hash)

{
level: severity,
time: time,
progname: progname,
}.merge(data).to_json.concat("\r\n")
}.merge(format_data(data)).to_json.concat("\r\n")
end

def format_data(data)
Expand Down
36 changes: 36 additions & 0 deletions packages/lumberaxe/spec/lumberaxe/lumberaxe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,40 @@
expect(Rails.logger.silence { "test_silencer" }).to eq("test_silencer")
end
end

context "tagged logging" do
subject do
Lumberaxe::Logger.new(progname: "tagged_logging")
end

it "logs the message" do
expect do
subject.tagged("rose") { subject.info("bud") }
end.to output(/"message":"bud"/).to_stdout_from_any_process
end

it "logs tags" do
expect do
subject.tagged("hot", "sour") { subject.info("soup") }
end.to output(/"tags":\["hot","sour"\]/).to_stdout_from_any_process
end

it "logs tags as named keys" do
expect do
subject.tagged("evel=knievel") { subject.info("parachute") }
end.to output(/"evel":"knievel"/).to_stdout_from_any_process
end

it "logs hash tags" do
expect do
subject.tagged(hash: "alton") { subject.info("brown") }
end.to output(/"tags":\[{"hash":"alton"}\]/).to_stdout_from_any_process
end

it "logs any valid combination of tag formats" do
expect do
subject.tagged("omega", "pV=nRT", paink: "iller") { subject.info("brown") }
end.to output(/(?=.*"tags":\["omega",{"paink":"iller"}\])(?=.*"pV":"nRT")/).to_stdout_from_any_process
end
end
end

0 comments on commit 9bf3e54

Please sign in to comment.