module PgDice::LogHelper

LogHelper provides a convenient wrapper block to log out the duration of an operation

Public Class Methods

log_duration(message, logger, options = {}) { || ... } click to toggle source

If you want to pass the the result of your block into the message you can use '{}' and it will be substituted with the result of your block.

# File lib/pgdice/log_helper.rb, line 18
def log_duration(message, logger, options = {})
  logger.error { 'log_duration called without a block. Cannot time the duration of nothing.' } unless block_given?
  time_start = Time.now.utc
  result = yield
  time_end = Time.now.utc

  formatted_message = format_message(time_end, time_start, message, result)
  logger.public_send(options[:log_level] || :debug) { formatted_message }
  result
end

Private Class Methods

format_message(time_end, time_start, message, result) click to toggle source
# File lib/pgdice/log_helper.rb, line 31
def format_message(time_end, time_start, message, result)
  message = message.sub(/{}/, result.to_s)
  "#{message} took: #{format('%.02f', (time_end - time_start))} seconds."
end

Public Instance Methods

blank?(string) click to toggle source
# File lib/pgdice/log_helper.rb, line 6
def blank?(string)
  string.nil? || string.empty?
end
squish(string) click to toggle source
# File lib/pgdice/log_helper.rb, line 10
def squish(string)
  string ||= ''
  string.gsub(/\s+/, ' ')
end