Class: ExceptionLog::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_log/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, exception, options = {}) ⇒ Handler

Returns a new instance of Handler.



7
8
9
10
11
12
13
# File 'lib/exception_log/handler.rb', line 7

def initialize(env,exception,options={})
  @exception = exception
  @request = ActionDispatch::Request.new(env)
  @controller = env['action_controller.instance']
  @options = options
  @body = generate_text
end

Instance Method Details

#default_log_pathObject



41
42
43
# File 'lib/exception_log/handler.rb', line 41

def default_log_path
  "#{Rails.root}/log/exception.log"
end

#doObject



56
57
58
59
# File 'lib/exception_log/handler.rb', line 56

def do
  write_file
  send_mail
end

#generate_textObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/exception_log/handler.rb', line 15

def generate_text
  org_str = if @controller.respond_to? :current_orgnization
    org = @controller.send(:current_orgnization)
    "orgnization: #{org.try(:id)} #{org.try(:name)}"
  end
  user_str = if @controller.respond_to? :current_user
    user = @controller.send(:current_user)
    "user: #{user.try(:id)} #{user.try(:name)}"
  end
  error ="\\n\\n\\n========================================= EXCEPTION ==============================================================      \n\#{org_str}\n\#{user_str}\ntime: \#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}\nmethod: \#{@request.method}\nurl: \#{@request.url}\nuser-agent: \#{@request.user_agent}\nreferer: \#{@request.headers[\"Referer\"]}\nparams: \#{@controller.params}\nexception: \#{@exception.class}\nmessage: \#{@exception.message}\nbacktace: \\n\#{@exception.backtrace.join(\"\\n\")}\n"
return error
end

#send_mailObject



52
53
54
# File 'lib/exception_log/handler.rb', line 52

def send_mail
  ExceptionLog::Mailer.exception_mail(@body,:from=>@options[:from],:to=>@options[:to]).deliver
end

#write_fileObject



45
46
47
48
49
50
# File 'lib/exception_log/handler.rb', line 45

def write_file
  log_path = @options[:log_path] || default_log_path
  File.open(log_path,"a+") do |f|
    f.puts @body
  end
end