-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Rails.logger and ActiveSupport::Notifications for logging instead…
… of puts (#10)
- Loading branch information
1 parent
fa161ab
commit 889d036
Showing
7 changed files
with
102 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module GoodJob::Logging | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
cattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) | ||
|
||
def self.tag_logger(*tags) | ||
if logger.respond_to?(:tagged) | ||
tags.unshift "GoodJob" unless logger.formatter.current_tags.include?("GoodJob") | ||
logger.tagged(*tags) { yield } | ||
else | ||
yield | ||
end | ||
end | ||
end | ||
|
||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
def create(event) | ||
good_job = event.payload[:good_job] | ||
|
||
info do | ||
"Created GoodJob resource with id #{good_job.id}" | ||
end | ||
end | ||
|
||
def timer_task_finished(event) | ||
result = event.payload[:result] | ||
exception = event.payload[:error] | ||
|
||
if exception | ||
error do | ||
"ERROR: #{exception}\n #{exception.backtrace}" | ||
end | ||
end | ||
end | ||
|
||
def job_finished(event) | ||
result = event.payload[:result] | ||
exception = event.payload[:error] | ||
|
||
if exception | ||
error do | ||
"ERROR: #{exception}\n #{exception.backtrace}" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def logger | ||
GoodJob.logger | ||
end | ||
|
||
def thread_name | ||
Thread.current.name || Thread.current.object_id | ||
end | ||
end | ||
end | ||
|
||
GoodJob::Logging::LogSubscriber.attach_to :good_job |
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,4 +1,7 @@ | ||
module GoodJob | ||
class Railtie < ::Rails::Railtie | ||
initializer "good_job.logger" do | ||
ActiveSupport.on_load(:good_job) { self.logger = ::Rails.logger } | ||
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,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::Railtie do | ||
it 'copies over the Rails logger by default' do | ||
expect(GoodJob.logger).to eq Rails.logger | ||
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