From 08fe29c842b785dbbdb2edfd41a0cb47bec0b212 Mon Sep 17 00:00:00 2001 From: Dick Davis Date: Sat, 10 Aug 2024 21:05:00 -0500 Subject: [PATCH] Refactor emitter to allow configurable logger classes --- lib/event_logger_rails.rb | 9 ++++++--- lib/event_logger_rails/emitter.rb | 9 +++------ spec/lib/event_logger_rails/emitter_spec.rb | 2 +- spec/lib/event_logger_rails_spec.rb | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/event_logger_rails.rb b/lib/event_logger_rails.rb index ee8ccc0..c2bf0bd 100644 --- a/lib/event_logger_rails.rb +++ b/lib/event_logger_rails.rb @@ -26,6 +26,10 @@ module EventLoggerRails # @return [IO, #write] The log device used by EventLoggerRails. mattr_accessor :logdev + # @!attribute [r] logger_class + # @return [Class] The logger class used by EventLoggerRails. + mattr_accessor :logger_class + # @!attribute [r] registered_events # @return [Array] The events registry defined in the config/event_logger_rails.yml file. mattr_accessor :registered_events @@ -48,10 +52,9 @@ def self.setup # Returns or initializes the Emitter instance for EventLoggerRails. # - # @note The emitter is initialized with the configured log device. # @return [Emitter] The Emitter instance used for logging events. def self.emitter - @emitter ||= Emitter.new(logdev:) + @emitter ||= Emitter.new end # Forwards the arguments to the Emitter's log method. @@ -59,7 +62,7 @@ def self.emitter # @example # EventLoggerRails.log('foo.bar.baz', level: :info, data: { foo: 'bar' }) # @param (see Emitter#log) - # @return [void] + # @return (see Emitter#log) def self.log(...) emitter.log(...) end diff --git a/lib/event_logger_rails/emitter.rb b/lib/event_logger_rails/emitter.rb index 196b408..9d5588a 100644 --- a/lib/event_logger_rails/emitter.rb +++ b/lib/event_logger_rails/emitter.rb @@ -1,14 +1,11 @@ # frozen_string_literal: true module EventLoggerRails - ## # Processes events, sending data to logger. class Emitter ## Initializes the emitter using the given log device for log output. - # - # @param logdev [IO, #write] The log device for log output. - def initialize(logdev:) - @logger = JsonLogger.new(logdev) + def initialize + @logger = EventLoggerRails.logger_class.safe_constantize.new(EventLoggerRails.logdev) end # Validates and logs an event with the given level and data. @@ -20,7 +17,7 @@ def initialize(logdev:) # @param data [Hash] Additional data to log. # @return [Integer] The number of bytes written to the log. # @example - # emitter = EventLoggerRails::Emitter.new(logdev: $stdout) + # emitter = EventLoggerRails::Emitter.new # emitter.log('foo.bar.baz', level: :info, data: { foo: 'bar' }) def log(event, level:, data: {}) Event.new(event).validate! do |validated_event| diff --git a/spec/lib/event_logger_rails/emitter_spec.rb b/spec/lib/event_logger_rails/emitter_spec.rb index f0183ed..ca0ffe0 100644 --- a/spec/lib/event_logger_rails/emitter_spec.rb +++ b/spec/lib/event_logger_rails/emitter_spec.rb @@ -4,7 +4,7 @@ RSpec.describe EventLoggerRails::Emitter do subject(:emitter) do - described_class.new(logdev: File.open(File::NULL, 'w')) + described_class.new end describe '#log' do diff --git a/spec/lib/event_logger_rails_spec.rb b/spec/lib/event_logger_rails_spec.rb index e460149..9d62f56 100644 --- a/spec/lib/event_logger_rails_spec.rb +++ b/spec/lib/event_logger_rails_spec.rb @@ -36,6 +36,21 @@ end end + describe 'logger_class' do + subject(:engine_setup) do + engine.setup do |config| + config.logger_class = logger_class + end + end + + let(:logger_class) { 'EventLoggerRails::JsonLogger' } + + it 'configures the output device to use for initializing the logger' do + engine_setup + expect(described_class.logger_class).to eq(logger_class) + end + end + describe 'registered events' do subject(:engine_setup) do engine.setup do |config|