diff --git a/lib/cloud_controller/runners/puma_runner.rb b/lib/cloud_controller/runners/puma_runner.rb index 1211a1457d..f1d378dafb 100644 --- a/lib/cloud_controller/runners/puma_runner.rb +++ b/lib/cloud_controller/runners/puma_runner.rb @@ -1,7 +1,7 @@ require 'puma' require 'puma/configuration' require 'puma/events' -require 'cloud_controller/logs/steno_io' +require 'steno/steno' require 'cloud_controller/execution_context' module VCAP::CloudController @@ -51,7 +51,7 @@ def initialize(config, app, logger, periodic_updater, request_logs) conf.before_worker_shutdown { request_logs.log_incomplete_requests if request_logs } end - log_writer = Puma::LogWriter.new(StenoIO.new(logger, :info), StenoIO.new(logger, :error)) + log_writer = Puma::LogWriter.new(Steno::LoggerIO.new(logger, :info), Steno::LoggerIO.new(logger, :error)) events = Puma::Events.new events.after_booted { @periodic_updater.setup_updates } diff --git a/lib/steno/README.md b/lib/steno/README.md index b69bd4529c..1a408ec75e 100644 --- a/lib/steno/README.md +++ b/lib/steno/README.md @@ -23,7 +23,7 @@ This is a **modified version** of the original Steno library, adapted for CCNG's - Custom RFC3339 codec (previously in `lib/steno_custom_codec_temp/`) - LICENSE file (Apache 2.0 - for attribution) - NOTICE file (Apache 2.0 copyright notices) -- Test suite (in `spec/unit/lib/steno/`) +- Test suite (in `spec/isolated_specs/steno/`) ## What Was Excluded @@ -66,7 +66,7 @@ Syslog.close if Syslog.opened? - Required adding `require 'active_support/core_ext/module/delegation'` ### 4. Test Structure Updates -- Moved tests from `lib/steno/spec/` to `spec/unit/lib/steno/` (CCNG convention) +- Moved tests from `lib/steno/spec/` to `spec/isolated_specs/steno/` (CCNG convention) - Removed global spec_helper requires, added explicit requires per test - Wrapped test describes in parent `RSpec.describe` blocks @@ -75,7 +75,7 @@ Syslog.close if Syslog.opened? **Removed**: - `lib/steno/sink/eventlog.rb` (Windows Event Log sink) -- `spec/unit/lib/steno/unit/sink/eventlog_spec.rb` +- `spec/isolated_specs/steno/unit/sink/eventlog_spec.rb` - Windows conditionals from config.rb, syslog.rb, test files - `WINDOWS` constant from sink/base.rb @@ -86,8 +86,8 @@ Syslog.close if Syslog.opened? - `lib/steno/version.rb` - Version constant no longer needed (not a gem) - `lib/steno/json_prettifier.rb` - CLI tool for prettifying logs, unused (~110 lines) - `lib/steno/core_ext.rb` - Monkey patches for `.logger` on Module/Class/Object, CCNG uses `Steno.logger()` instead (~50 lines) -- `spec/unit/lib/steno/unit/json_prettifier_spec.rb` - Tests for removed feature -- `spec/unit/lib/steno/unit/core_ext_spec.rb` - Tests for removed feature +- `spec/isolated_specs/steno/unit/json_prettifier_spec.rb` - Tests for removed feature +- `spec/isolated_specs/steno/unit/core_ext_spec.rb` - Tests for removed feature - Removed `require 'steno/version'` from steno.rb **What's kept** (even if unused): @@ -120,6 +120,16 @@ Syslog.close if Syslog.opened? - `Steno::Context::Null` - Default fallback context - All log levels including debug1, debug2 - debug2 is actively used +### 8. LoggerIO Adapter for Puma Integration +**Rationale**: `Puma::LogWriter` requires IO-compatible objects. Previously, a `StenoIO` class existed outside the Steno library to bridge this gap. Since Steno is now inlined, the adapter was moved into the Steno namespace for consistency. + +**Added**: +- `lib/steno/logger_io.rb`: `Steno::LoggerIO` - an IO adapter that wraps a `Steno::Logger` and forwards `write`/`puts` calls to it at a configured log level. Used by `PumaRunner` to pass Steno loggers as IO objects to `Puma::LogWriter`. +- `spec/isolated_specs/steno/unit/logger_io_spec.rb`: Tests for `Steno::LoggerIO`. + +**Modified**: +- `lib/steno/logger.rb`: Broadened the callstack filter from removing only frames from `logger.rb` to removing all frames within `lib/steno/`. This ensures that log records originating via `Steno::LoggerIO` correctly attribute the source location to the caller rather than to the adapter itself. + --- ## Making Future Modifications @@ -129,7 +139,7 @@ If you modify this integrated steno library: 1. **Document changes** in the "Modifications Made After Integration" section above 2. **Include rationale** for why the change was made 3. **List affected files** and what changed -4. **Run tests** to ensure nothing breaks: `bundle exec rspec spec/unit/lib/steno/` +4. **Run tests** to ensure nothing breaks: `bundle exec rspec spec/isolated_specs/steno/` 5. **Update this README** in the same commit as your changes ## Original Steno Commit History (102 commits) diff --git a/lib/steno/logger.rb b/lib/steno/logger.rb index 0827618d19..10db7983c2 100644 --- a/lib/steno/logger.rb +++ b/lib/steno/logger.rb @@ -128,21 +128,11 @@ def log(level_name, message=nil, user_data=nil) private def parse_record_loc(callstack) - file = nil - lineno = nil - method = nil + frame = callstack.find { |f| f !~ %r{/lib/steno/} } || callstack.last - callstack.each do |frame| - next if frame =~ /logger\.rb/ - - file, lineno, method = frame.split(':') - - lineno = lineno.to_i - - method = ::Regexp.last_match(1) if method =~ /in `([^']+)/ - - break - end + file, lineno, method = frame.split(':') + lineno = lineno.to_i + method = ::Regexp.last_match(1) if method =~ /in `([^`']+)/ [file, lineno, method] end diff --git a/lib/cloud_controller/logs/steno_io.rb b/lib/steno/logger_io.rb similarity index 88% rename from lib/cloud_controller/logs/steno_io.rb rename to lib/steno/logger_io.rb index a72359ea6d..3e1993718e 100644 --- a/lib/cloud_controller/logs/steno_io.rb +++ b/lib/steno/logger_io.rb @@ -1,4 +1,7 @@ -class StenoIO +module Steno +end + +class Steno::LoggerIO def initialize(logger, level) @logger = logger @level = level diff --git a/lib/steno/steno.rb b/lib/steno/steno.rb index 955cf9ef36..7ebabbc27e 100644 --- a/lib/steno/steno.rb +++ b/lib/steno/steno.rb @@ -3,6 +3,7 @@ require 'steno/context' require 'steno/errors' require 'steno/log_level' +require 'steno/logger_io' require 'steno/logger' require 'steno/record' require 'steno/sink' diff --git a/spec/isolated_specs/steno/unit/logger_io_spec.rb b/spec/isolated_specs/steno/unit/logger_io_spec.rb new file mode 100644 index 0000000000..a0ca39e141 --- /dev/null +++ b/spec/isolated_specs/steno/unit/logger_io_spec.rb @@ -0,0 +1,34 @@ +require 'steno/steno' + +RSpec.describe Steno::LoggerIO do + let(:logger) { double(:logger) } + let(:level) { :info } + let(:logger_io) { described_class.new(logger, level) } + + describe '#write' do + it 'writes to the logger' do + expect(logger).to receive(:log).with(level, 'message') + + logger_io.write('message') + end + end + + describe '#sync' do + it 'returns true' do + expect(logger_io.sync).to be(true) + end + end + + context 'when writing a record' do + let(:logger) { Steno::Logger.new('test', []) } + + it 'removes logger_io.rb from the callstack' do + expect(Steno::Record).to receive(:new).and_wrap_original do |original_method, source, log_level, message, loc, data| + expect(loc[0]).not_to match(/logger_io\.rb/) + original_method.call(source, log_level, message, loc, data) + end + + logger_io.write('message') + end + end +end diff --git a/spec/isolated_specs/steno/unit/logger_spec.rb b/spec/isolated_specs/steno/unit/logger_spec.rb index c805db813d..d30e9b3947 100644 --- a/spec/isolated_specs/steno/unit/logger_spec.rb +++ b/spec/isolated_specs/steno/unit/logger_spec.rb @@ -74,13 +74,18 @@ end it 'creates a record with the proper level' do - sink = double('sink') expect(Steno::Record).to receive(:new).with('test', :warn, 'message', anything, anything).and_call_original - allow(sink).to receive(:add_record) - my_logger = described_class.new('test', [sink]) + logger.warn('message') + end + + it 'creates a record with logger.rb being removed from the callstack' do + expect(Steno::Record).to receive(:new).and_wrap_original do |original_method, source, log_level, message, loc, data| + expect(loc[0]).not_to match(/logger\.rb/) + original_method.call(source, log_level, message, loc, data) + end - my_logger.warn('message') + logger.warn('message') end end diff --git a/spec/unit/lib/cloud_controller/logs/steno_io_spec.rb b/spec/unit/lib/cloud_controller/logs/steno_io_spec.rb deleted file mode 100644 index 3b871ccf8b..0000000000 --- a/spec/unit/lib/cloud_controller/logs/steno_io_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'spec_helper' - -module VCAP::CloudController::Logs - RSpec.describe StenoIO do - let(:logger) { double(:logger) } - let(:level) { :info } - - subject { StenoIO.new(logger, level) } - - describe '#write' do - it 'writes to the logger' do - expect(logger).to receive(:log).with(level, 'message') - - subject.write('message') - end - end - - describe '#sync' do - it 'returns true' do - expect(subject.sync).to be(true) - end - end - end -end