Sha256: 07f99d87ad3b93babfb420f499ec4a155fc9e1d91370bdda4b061a618a0aeea0

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module Quandl
module Command
class Task

module Logging
  
  extend ActiveSupport::Concern

  included do
    before_call :start_request_timer
    before_execute :configure_logger
    after_call :log_request_time
    
    attr_accessor :request_timer
    
  end
  
  def start_request_timer
    self.request_timer = Time.now
  end
  
  def configure_logger
    @stderr_logger = initialize_logger(config.stderr) if config.stderr.present?
    @stdout_logger = initialize_logger(config.stdout) if config.stdout.present?
  end
  
  def initialize_logger(path)
    l = ::Logger.new( path, 2, 52428800 )
    l.formatter = proc do |severity, datetime, progname, msg|
      "# [#{datetime.strftime("%Y-%m-%d %H:%M:%S")}]\n#{msg}"
    end
    l
  end
  
  def log_request_time
    debug("Started: #{request_timer}. Finished: #{Time.now}. Elapsed: #{request_timer.elapsed_ms}")
  end
  
  def summarize(item)
    return summarize_hash(item) if item.kind_of?(Hash)
    item
  end
  
  def summarize_hash(item)
    item.collect do |k,v|
      next "#{k}: '#{v}'" if v.kind_of?(String)
      "#{k}: #{v}"
    end.join(', ')
  end
  
  def table(*args)
    Array(args).flatten.join(" | ")
  end
  
  def info(*args)
    logger.info(*args)
  end

  def debug(*args)
    args.each{|a| logger.debug("# #{a}") } if verbose?
  end

  def error(*args)
    stderr_logger.error(*args)
  end

  def fatal(*args)
    stderr_logger.fatal("FATAL: #{args.join(" ")}")
  end

  def logger
    @stdout_logger ||= Quandl::Logger
  end
  
  def stderr_logger
    @stderr_logger ||= Quandl::Logger
  end
  
end

end
end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
quandl-0.4.4 lib/quandl/command/task/logging.rb
quandl-0.4.3 lib/quandl/command/task/logging.rb
quandl-0.4.2 lib/quandl/command/task/logging.rb
quandl-0.4.1 lib/quandl/command/task/logging.rb
quandl-0.4.0 lib/quandl/command/task/logging.rb
quandl-0.3.7 lib/quandl/command/task/logging.rb
quandl-0.3.6 lib/quandl/command/task/logging.rb