Created
June 14, 2016 07:53
-
-
Save rainchen/ed170d57dfaa6d1f2585642b59e1f923 to your computer and use it in GitHub Desktop.
logger example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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