Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a error logger exception handler #2387

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/alchemy/error_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def self.call(exception)
end

mattr_accessor :notification_handler
@@notification_handler = BaseHandler
end
end

require "alchemy/error_tracking/error_logger"
Alchemy::ErrorTracking.notification_handler = Alchemy::ErrorTracking::ErrorLogger
13 changes: 13 additions & 0 deletions lib/alchemy/error_tracking/error_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Alchemy
module ErrorTracking
class ErrorLogger < BaseHandler
def self.call(exception)
::Rails.logger.tagged("alchemy_cms") do
::Rails.logger.error("#{exception.class.name}: #{exception.message} in #{exception.backtrace.first}")
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/libraries/alchemy/error_tracking/error_logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::ErrorTracking::ErrorLogger do
describe ".call" do
it "logs the exception into Rails logger with alchemy_cms tag" do
expect(::Rails.logger).to receive(:tagged).with("alchemy_cms").and_yield
expect(::Rails.logger).to receive(:error).with("RuntimeError: foo in /foo/bar.rb:1")
error = RuntimeError.new("foo")
error.set_backtrace(["/foo/bar.rb:1"])
described_class.call(error)
end
end
end