Skip to content

Instantly share code, notes, and snippets.

@rainchen
Created June 14, 2016 07:53
Show Gist options
  • Select an option

  • Save rainchen/ed170d57dfaa6d1f2585642b59e1f923 to your computer and use it in GitHub Desktop.

Select an option

Save rainchen/ed170d57dfaa6d1f2585642b59e1f923 to your computer and use it in GitHub Desktop.

Revisions

  1. rainchen created this gist Jun 14, 2016.
    16 changes: 16 additions & 0 deletions statistic.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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