Loading lib/labkit/logging/json_logger.rb +14 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,15 @@ module Labkit ENV.fetch("GITLAB_LOG_LEVEL", fallback) end def self.exclude_context! @exclude_context = true self end def self.exclude_context? !!@exclude_context end def initialize(path, level: JsonLogger.log_level) super end Loading @@ -31,7 +40,12 @@ module Labkit data = default_attributes data[:severity] = severity data[:time] = timestamp.utc.iso8601(3) if self.class.exclude_context? data[Labkit::Correlation::CorrelationId::LOG_KEY] = Labkit::Correlation::CorrelationId.current_id else data.merge!(Labkit::Context.current.to_h) end case message when String Loading spec/labkit/logging/json_logger_spec.rb +51 −4 Original line number Diff line number Diff line Loading @@ -7,8 +7,8 @@ RSpec.describe Labkit::Logging::JsonLogger do let(:now) { Time.now } before do allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return("new-correlation-id") around do |example| Labkit::Context.with_context { example.run } end describe ".initialize" do Loading Loading @@ -107,7 +107,7 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["severity"]).to eq("INFO") expect(data["time"]).to eq(now.utc.iso8601(3)) expect(data["message"]).to eq("Hello world") expect(data["correlation_id"]).to eq("new-correlation-id") expect(data["correlation_id"]).to be_present end it "formats hashes" do Loading @@ -118,7 +118,7 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["time"]).to eq(now.utc.iso8601(3)) expect(data["hello"]).to eq(1) expect(data["message"]).to be_nil expect(data["correlation_id"]).to eq("new-correlation-id") expect(data["correlation_id"]).to be_present end it "uses indifferent log key access" do Loading @@ -129,6 +129,53 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["world"]).to eq(2) end it "includes the logging context in the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", "Log message") data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, "#{Labkit::Context::LOG_KEY}.hello" => "world", ) end end it "does not override logging context passed directly into the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", { "#{Labkit::Context::LOG_KEY}.hello" => "brave world" }) data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, "#{Labkit::Context::LOG_KEY}.hello" => "brave world", ) end end context "when exclude_context is set" do let(:logger) do Class.new(described_class) do exclude_context! end end subject { logger.new("/dev/null") } it "does not includes the logging context in the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", "Log message") data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, ) expect(data).not_to include( "#{Labkit::Context::LOG_KEY}.hello" => "world", ) end end end describe "reserved log keys" do let(:reserved_key_data) { described_class::RESERVED_LOG_KEYS.to_h { |k| [k, 42] } } let(:expected_error) { /^The following log keys used are reserved: #{described_class::RESERVED_LOG_KEYS.join(", ")}.*/ } Loading Loading
lib/labkit/logging/json_logger.rb +14 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,15 @@ module Labkit ENV.fetch("GITLAB_LOG_LEVEL", fallback) end def self.exclude_context! @exclude_context = true self end def self.exclude_context? !!@exclude_context end def initialize(path, level: JsonLogger.log_level) super end Loading @@ -31,7 +40,12 @@ module Labkit data = default_attributes data[:severity] = severity data[:time] = timestamp.utc.iso8601(3) if self.class.exclude_context? data[Labkit::Correlation::CorrelationId::LOG_KEY] = Labkit::Correlation::CorrelationId.current_id else data.merge!(Labkit::Context.current.to_h) end case message when String Loading
spec/labkit/logging/json_logger_spec.rb +51 −4 Original line number Diff line number Diff line Loading @@ -7,8 +7,8 @@ RSpec.describe Labkit::Logging::JsonLogger do let(:now) { Time.now } before do allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return("new-correlation-id") around do |example| Labkit::Context.with_context { example.run } end describe ".initialize" do Loading Loading @@ -107,7 +107,7 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["severity"]).to eq("INFO") expect(data["time"]).to eq(now.utc.iso8601(3)) expect(data["message"]).to eq("Hello world") expect(data["correlation_id"]).to eq("new-correlation-id") expect(data["correlation_id"]).to be_present end it "formats hashes" do Loading @@ -118,7 +118,7 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["time"]).to eq(now.utc.iso8601(3)) expect(data["hello"]).to eq(1) expect(data["message"]).to be_nil expect(data["correlation_id"]).to eq("new-correlation-id") expect(data["correlation_id"]).to be_present end it "uses indifferent log key access" do Loading @@ -129,6 +129,53 @@ RSpec.describe Labkit::Logging::JsonLogger do expect(data["world"]).to eq(2) end it "includes the logging context in the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", "Log message") data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, "#{Labkit::Context::LOG_KEY}.hello" => "world", ) end end it "does not override logging context passed directly into the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", { "#{Labkit::Context::LOG_KEY}.hello" => "brave world" }) data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, "#{Labkit::Context::LOG_KEY}.hello" => "brave world", ) end end context "when exclude_context is set" do let(:logger) do Class.new(described_class) do exclude_context! end end subject { logger.new("/dev/null") } it "does not includes the logging context in the message" do Labkit::Context.with_context("hello" => "world") do output = subject.format_message("INFO", now, "test", "Log message") data = JSON.parse(output) expect(data).to include( "correlation_id" => Labkit::Correlation::CorrelationId.current_id, ) expect(data).not_to include( "#{Labkit::Context::LOG_KEY}.hello" => "world", ) end end end describe "reserved log keys" do let(:reserved_key_data) { described_class::RESERVED_LOG_KEYS.to_h { |k| [k, 42] } } let(:expected_error) { /^The following log keys used are reserved: #{described_class::RESERVED_LOG_KEYS.join(", ")}.*/ } Loading