module AutoLogger

Чтобы указать имя лог файла используйте AutoLogger::Named:

class CurrencyRatesWorker include AutoLogger::Named.new(name: 'filename')

Source: blog.bigbinary.com/2014/03/03/logger-formatting-in-rails.html

Constants

DEFAULT_LOG_DIR
VERSION

Public Instance Methods

logger() click to toggle source
# File lib/auto_logger.rb, line 33
def logger
  _cached_logger ||= _build_auto_logger
end

Private Instance Methods

_auto_logger_file() click to toggle source
# File lib/auto_logger.rb, line 52
def _auto_logger_file
  file = "#{_auto_logger_tag}.log"

  if log_dir.present?
    File.join(log_dir, file)

  elsif defined? Rails
    Rails.root.join 'log', file

  else
    File.join(DEFAULT_LOG_DIR, file)
  end
end
_auto_logger_tag() click to toggle source
# File lib/auto_logger.rb, line 48
def _auto_logger_tag
  ([Class, Module].include?(self.class) ? self.name : self.class.name).underscore.gsub('/','_')
end
_build_auto_logger() click to toggle source
# File lib/auto_logger.rb, line 70
def _build_auto_logger
  if logger_builder.nil?
    ActiveSupport::Logger.new(_auto_logger_file).
      tap { |logger| logger.formatter = _log_formatter }
  else
    logger_builder.call(_auto_logger_tag, _log_formatter)
  end
end
_log_formatter() click to toggle source
# File lib/auto_logger.rb, line 66
def _log_formatter
  @log_formatter || !defined?(Rails) || Rails.env.test? ? Logger::Formatter.new : Formatter.new
end
bm_log(message) { || ... } click to toggle source

Логируем вместе с временем выполнения

# File lib/auto_logger.rb, line 41
def bm_log(message)
  res = nil
  bm = Benchmark.measure { res = yield }
  logger.info "#{message}: #{bm}"
  res
end