Skip to content

Instantly share code, notes, and snippets.

@rainchen
Created June 14, 2016 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainchen/ed170d57dfaa6d1f2585642b59e1f923 to your computer and use it in GitHub Desktop.
Save rainchen/ed170d57dfaa6d1f2585642b59e1f923 to your computer and use it in GitHub Desktop.
logger example
class Statistic
# save message to a log file
# usage:
# Statistic.log "message"
# Statistic.log { var }
def self.log(message = nil, &block)
@logger ||= Logger.new(Rails.root.join('log', "statistic.#{Rails.env}.log"))
begin
message = yield if block_given?
@logger.info("#{Time.current} #{message}")
rescue Exception => e
# make sure the log method will not be stop the main progress
@logger.info("#{Time.current} error:#{e} for #{block}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment