Last active
August 7, 2020 12:37
-
-
Save lujanfernaud/0a2b52956335ac6996cf7c6efe5d8ac0 to your computer and use it in GitHub Desktop.
Debugging: LogInteractorExecution
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
# Usage: | |
# | |
# class SomeInteractor | |
# include Interactor | |
# include LogInteractorExecution | |
# | |
# # ... | |
# | |
# private | |
# | |
# def some_method | |
# log_method_start(__method__) | |
# # ... | |
# log_method_finish(__method__) | |
# end | |
# end | |
# | |
module LogInteractorExecution | |
LOG_HANDLE = 'LogInteractorExecution'.freeze | |
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | |
def self.included(base) | |
base.class_eval do | |
around do |interactor| | |
log_interactor_start(base) | |
context.start_time = Time.now | |
interactor.call | |
context.finish_time = Time.now | |
context.time_in_seconds = context.finish_time - context.start_time | |
log_interactor_finish(base, context) | |
end | |
def log_interactor_start(base) | |
start_message = "#{LOG_HANDLE}: STARTED #{base} -- Started at #{Time.now}" | |
Rails.logger.info(start_message) | |
end | |
def log_interactor_finish(base, context) | |
finish_message = "#{LOG_HANDLE}: FINISHED #{base} -- Finished in #{context.time_in_seconds} seconds" | |
Rails.logger.info(finish_message) | |
end | |
def log_method_start(method) | |
Rails.logger.info("#{LOG_HANDLE}: Started ##{method}") | |
end | |
def log_method_finish(method) | |
Rails.logger.info("#{LOG_HANDLE}: Finished ##{method}") | |
end | |
end | |
end | |
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment