Create a gist now

Instantly share code, notes, and snippets.

@jerrod /chores_and_logging.rb Secret
Last active Mar 23, 2016

What would you like to do?
UserTesting Chores
module Chores
module Logging
attr_reader :description
def with_logging(description="", &block)
@description = description
log_info "###### #{description} START: #{Time.now}\n"
block.call log
log_close
log_flush
end
delegate :log_info, :log_close, :log_flush, to: :log
private
def log
@log ||= Chores::Log.new(description).create
end
end
end
class Chores::Log
attr_reader :log_file
def initialize(description)
@description = description
end
def create
@log_file ||= File.open(local_file_path, 'w')
self
end
def log_info(content)
log_file.write(content)
end
alias_method :log, :log_info
def log_close
log_info "###### #{description} END: #{Time.now}\n"
log_file.close
end
def log_flush
send_to_s3
File.delete(local_file_path)
end
private
def bucket_name
"chore-logs-#{Rails.env}"
end
def file_name
"#{timestamp}-#{parameterized_description}.log"
end
def parameterized_description
description.downcase.gsub(/\W/, ' ').strip.gsub(' ', '-')
end
def local_file_path
@local_file_path ||= File.join(Rails.root, 'tmp', file_name)
end
def timestamp
@timestamp ||= DateTime.current.utc.strftime("%Y%m%d-%H%M")
end
def send_to_s3
AwsClient.new(file_name, local_file_path, bucket_name).save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment