-
Notifications
You must be signed in to change notification settings - Fork 646
Description
When defining a log to emit to, it does not seem to be respected in our app.
I spent today trying to get our app to push the STDERR warnings into a SemanticLogger['AASM'] instance with no luck.
We have defined a state_machine.rb module like so:
module Dubbing
# State machine default logic for the dubbing workflow
#
# This adds in common logging logic so we can easily monitor and troubleshoot state changes with our logs.
module StateMachine
extend ActiveSupport::Concern
included do
include AASM
aasm column: :status, enum: true do
after_all_transitions :log_status_change
event :restart do
transitions from: :failed, to: :request_received
end
error_on_all_events :log_event_errors
end
def log_event_errors(error)
log_aasm_message("Error: #{error.message}", level: :warn)
end
end
private
def log_status_change
log_message = [
'Status Change',
"from: #{aasm.from_state}",
"to: #{aasm.to_state}"
]
log(log_message.join(', '))
end
def log_aasm_message(message, level: :info)
log_message = [
"message: #{message}",
"to_state: #{aasm.to_state}"
]
log(log_message.join(', '), level: level)
end
def log(message, level: :info)
log_message = [
"AASM - #{self.class.name}:#{self.id}",
message,
"current_event: #{aasm.current_event}"
]
logger.send(level, log_message.join(', '))
end
end
endWhen I run my RSpec test suite we have a bunch of methods that intentionally emit warn level log messages through this class like so:
aasm column: :status, enum: true do
state :request_received, initial: true
state :collecting_assets
state :transfer_initiated
state :assets_collected
state :order_sent
state :order_received
state :complete
state :failed
...
event :fail, after: Proc.new { |*args| log_aasm_message(args[0], level: :warn) } do
transitions to: :failed
end
end
def trigger_asset_collection!
if collect_assets!
success = dubbing_request_projects.inject(true) { |result, drp| result && drp.trigger_asset_collection! }
return transfer_initiated! if success
end
log_aasm_message("Error During Asset Collection: Did not initiate transfer to partner")
return false
rescue => e
self.fail! "Error During Asset Collection: #{e.message}"
false
endI attempted to add the logger: SemanticLogger['AASM'] to the aasm block above, and while I didn't receive new objections or errors, it did not cause the output to STDERR to cease.
I also attempted to locally set it in the def log method as such:
def log
logger = SemanticLogger['AASM']
logger.send(level, log_message.join(', '))
endBut this caused my RSpec tests looking for the log output to fail. For example:
require 'rails_helper'
RSpec.describe Dubbing::TransferAssetsToPartnerJob, type: :job do
let(:job) { described_class.new }
describe '#perform' do
let(:dubbing_request_project) { create(:dubbing_request_project, status: :collecting_assets) }
let(:logger) { instance_double(SemanticLogger::Logger) }
before do
allow(logger).to receive(:info)
allow_any_instance_of(Dubbing::Request::Project).to receive(:logger).and_return(logger)
end
it 'changes dubbing_request_project state to failed when an exception is raised' do
allow_any_instance_of(Services::Dubbing::OrderWorkflow).to receive(:transfer_files_to_partner!)
.and_raise('bad transition!')
expect(logger).to receive(:warn).with(/Error During Asset Collection: bad transition!/)
expect { job.perform(dubbing_request_project.id) }
.to raise_error('bad transition!')
expect(dubbing_request_project.reload.status).to eq('failed')
end
end
endwould return:
2) Dubbing::TransferAssetsToPartnerJob#perform changes dubbing_request_project state to failed when an exception is raised
Failure/Error:
expect { job.perform(dubbing_request_project.id) }
.to raise_error('bad transition!')
expected Exception with "bad transition!", got #<NoMethodError: undefined method `semantic_logger_events' for #<Dubbing::Request::Project id: 2712, ...eated_at: "2025-03-26 20:50:49.401174000 +0000", updated_at: "2025-03-26 20:50:49.401174000 +0000">> with backtrace:
I also tried to simply monkey patch base.rb but it ended up throwing errors complaining about Rack middleware. Any advice is appreciated.