Commit f7dedf1c authored by Bob Van Landuyt's avatar Bob Van Landuyt 💬
Browse files

Merge branch 'include-context-by-default' into 'master'

Include context by default in JsonLogger

See merge request !112
parents deb841e4 29bba8df
Loading
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -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
@@ -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
+51 −4
Original line number Diff line number Diff line
@@ -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
@@ -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
@@ -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
@@ -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(", ")}.*/ }