Skip to content

Commit

Permalink
Refactor emitter to allow configurable logger classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dickdavis committed Aug 11, 2024
1 parent 822f0a7 commit 08fe29c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
9 changes: 6 additions & 3 deletions lib/event_logger_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash>] The events registry defined in the config/event_logger_rails.yml file.
mattr_accessor :registered_events
Expand All @@ -48,18 +52,17 @@ 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.
#
# @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
Expand Down
9 changes: 3 additions & 6 deletions lib/event_logger_rails/emitter.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/event_logger_rails/emitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/event_logger_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 08fe29c

Please sign in to comment.