-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to allow configurable logger and formatter classes
- Loading branch information
Showing
10 changed files
with
125 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 3 additions & 6 deletions
9
lib/event_logger_rails/json_logger.rb → lib/event_logger_rails/event_logger.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventLoggerRails | ||
# Writes log entries in JSON format | ||
class JsonLogger < ::Logger | ||
# Writes log entries in configured format. | ||
class EventLogger < ::Logger | ||
include ActiveSupport::LoggerSilence | ||
|
||
# Initializes the logger with a JSON formatter. | ||
# | ||
# @param logdev [IO, #write] The log device for log output. | ||
def initialize(...) | ||
super(...) | ||
@formatter = proc do |level, timestamp, _progname, message| | ||
output = Output.new(level:, timestamp:, message:) | ||
"#{output.to_json}\n" | ||
end | ||
@formatter = EventLoggerRails.formatter.constantize.new || EventLoggerRails::Formatters::JSON.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module EventLoggerRails | ||
module Formatters | ||
# Writes log entries in JSON format | ||
class JSON < ActiveSupport::Logger::SimpleFormatter | ||
# Initializes the logger with a JSON formatter. | ||
# | ||
# @param logdev [IO, #write] The log device for log output. | ||
def call(level, timestamp, _progname, message) | ||
output = Output.new(level:, timestamp:, message:) | ||
"#{output.to_json}\n" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe EventLoggerRails::EventLogger do | ||
subject(:event_logger) { described_class.new(File.open(File::NULL, 'w')) } | ||
|
||
let(:message) { { foo: 'bar' } } | ||
let(:buffer) { StringIO.new } | ||
|
||
before do | ||
allow(IO).to receive(:open).and_return(buffer) | ||
end | ||
|
||
context 'when default formatter is configured' do | ||
it 'outputs the provided message in JSON format' do | ||
event_logger.info(message) | ||
log_output = JSON.parse(buffer.string, symbolize_names: true) | ||
expect(log_output).to include(**message) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe EventLoggerRails::Formatters::JSON do | ||
subject(:formatter) { described_class.new } | ||
|
||
describe '#call' do | ||
subject(:method_call) { formatter.call(level, timestamp, progname, message) } | ||
|
||
let(:level) { :foo } | ||
let(:timestamp) { Time.now } | ||
let(:progname) { 'bar' } | ||
let(:message) { { foo: 'bar' } } | ||
|
||
it 'returns the event reserved for the exception' do | ||
expected_output = EventLoggerRails::Output.new(level:, timestamp:, message:).to_json | ||
expect(method_call).to eq("#{expected_output}\n") | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters