Commit 7e5dbbb7 authored by Matias Alvarez's avatar Matias Alvarez Committed by Elliot Forbes
Browse files

feat: Add any logger to the logging todo file

parent 9b3c7f21
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -17,6 +17,13 @@ module Labkit

        DEFAULT_CONTEXT_CALLSITE = "Labkit::Context"

        # Placeholder logger_class used when an offense originates from the Labkit
        # context. The same context fields appear in every logger that runs within
        # the context, so collapsing across loggers prevents one offense per
        # logger × deprecated_field combination — and prevents new offenses being
        # raised whenever a developer adds a new logger class.
        ANY_LOGGER = "*"

        class << self
          def register_wrapper_pattern(pattern)
            wrapper_patterns << pattern
@@ -82,14 +89,14 @@ module Labkit
                Registry.instance.record_offense(callsite_path, location.lineno, key_str, standard_field, logger_class)
              else
                Registry.instance.record_offense(
                  LogInterceptor.context_callsite, 0, key_str, standard_field, logger_class
                  LogInterceptor.context_callsite, 0, key_str, standard_field, ANY_LOGGER
                )
              end
            end
          end

          Registry.instance.check_for_removed_offenses(callsite_path, data, logger_class)
          Registry.instance.check_for_removed_offenses(LogInterceptor.context_callsite, data, logger_class)
          Registry.instance.check_for_removed_offenses(LogInterceptor.context_callsite, data, ANY_LOGGER)

          data
        end
+29 −1
Original line number Diff line number Diff line
@@ -114,6 +114,33 @@ RSpec.describe Labkit::Logging::FieldValidator::LogInterceptor do
        expect(offense['deprecated_field']).to eq('meta.user_id')
      end

      it 'records the offense under the ANY_LOGGER placeholder, not the actual class' do
        Labkit::Context.with_context('meta.user_id' => 456) do
          test_logger.format_message('INFO', Time.now.utc, 'test', { message: 'hello' })
        end

        expect(registry.offenses.first['logger_class'])
          .to eq(Labkit::Logging::FieldValidator::LogInterceptor::ANY_LOGGER)
      end

      it 'produces a single entry across multiple logger classes' do
        other_logger_class = Class.new(Labkit::Logging::JsonLogger) do
          prepend Labkit::Logging::FieldValidator::LogInterceptor
        end
        other_logger = other_logger_class.new(File::NULL)
        allow(other_logger).to receive(:determine_callsite).and_return(mock_location)

        Labkit::Context.with_context('meta.user_id' => 456) do
          test_logger.format_message('INFO', Time.now.utc, 'test', { message: 'hello' })
          other_logger.format_message('INFO', Time.now.utc, 'test', { message: 'hello' })
        end

        context_offenses = registry.offenses.select do |o|
          o['callsite'] == Labkit::Logging::FieldValidator::LogInterceptor.context_callsite
        end
        expect(context_offenses.size).to eq(1)
      end

      it 'records lineno 0 for context-originated offenses' do
        Labkit::Context.with_context('meta.user_id' => 456) do
          test_logger.format_message('INFO', Time.now.utc, 'test', { message: 'hello' })
@@ -169,7 +196,8 @@ RSpec.describe Labkit::Logging::FieldValidator::LogInterceptor do
          .with('app/test.rb', hash_including('gl_user_id' => 123, 'message' => 'hello'), 'AnonymousLogger')
        expect(registry).to receive(:check_for_removed_offenses)
          .with(Labkit::Logging::FieldValidator::LogInterceptor.context_callsite,
            hash_including('gl_user_id' => 123, 'message' => 'hello'), 'AnonymousLogger')
            hash_including('gl_user_id' => 123, 'message' => 'hello'),
            Labkit::Logging::FieldValidator::LogInterceptor::ANY_LOGGER)

        test_logger.format_message('INFO', Time.now.utc, 'test', { 'gl_user_id' => 123, 'message' => 'hello' })
      end