Module: ErrorLog::ClassMethods

Included in:
ErrorLog
Defined in:
lib/error_log/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#current_revisionObject



57
58
59
# File 'lib/error_log/class_methods.rb', line 57

def current_revision
  @current_revision
end

#initObject



52
53
54
55
# File 'lib/error_log/class_methods.rb', line 52

def init
  @current_revision = Vcs.revision
  Migrations.auto_migrate!
end

#log(level, error, options = {}) ⇒ Object

Log error, exception, or whatever you have on your mind

Args:

* level - error level {:debug, :info, :warn, :error, :fatal}
* error - error string itself
* options

Possible options:

* :backtrace 
* :params - params that can help you recreate this error, Hash
* :category - error category, helps you to group them later


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/error_log/class_methods.rb', line 22

def log(level,error,options={})
  backtrace = options[:backtrace]
  unless backtrace
    begin
      # I'm raising an exception just to get the current backtrace
      # If there is any more clever way to do that, please share.
      raise "wat?!"
    rescue Exception => e
      backtrace = e.backtrace[1..-1]
    end
  end

  # Silent rescues may not be optimal, for now they seem better than creating more mess
  logger.error "\n\n\n#{Time.now}\n= #{error}\n#{backtrace.join("\n")}\n#{options[:params]}" rescue nil

  ErrorLog::Model.create(
    :error => error,
    :backtrace => backtrace,
    :level => level,
    :params => options[:params],
    :vcs_revision => current_revision,
    :category => options[:category] || 'error_log'
  ) rescue nil 
end

#loggerObject

Errors log, may be useful when you’re facing some db related problems



48
49
50
# File 'lib/error_log/class_methods.rb', line 48

def logger
  @logger = Logger.new(File.join(Rails.root,'log','error.log'))
end

#optionsObject



4
5
6
7
8
# File 'lib/error_log/class_methods.rb', line 4

def options
  @options ||= {
    :vcs => Vcs.guess
  }
end