Class: ExceptionNotifier::GraylogNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_notifier/graylog_notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GraylogNotifier

Returns a new instance of GraylogNotifier.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/exception_notifier/graylog_notifier.rb', line 5

def initialize(options = {})
  standard_args = {
      :hostname => "localhost",
      :port => 12201,
      :local_app_name => Socket::gethostname,
      :facility => 'gelf_exceptions',
      :max_chunk_size => 'LAN',
      :level => 3
  }

  @args = standard_args.merge(options)
  @notifier = GELF::Notifier.new(@args[:hostname], @args[:port], @args[:max_chunk_size])
end

Instance Method Details

#call(err, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/exception_notifier/graylog_notifier.rb', line 19

def call(err, options={})
  env = options[:env]
  begin
    opts = {
        :short_message => err.message,
        :facility => @args[:facility],
        :level => @args[:level],
        :host => @args[:local_app_name],
        :app_name=>@args[:app_name]
    }

    if err.backtrace && err.backtrace.size > 0
      bc = ActiveSupport::BacktraceCleaner.new
      bc.add_filter { |line| line.gsub(Rails.root.to_s, '') }
      bc.add_silencer { |line| line =~ /mongrel|rvm\/gems|rubygems/ }
      _backtrace = bc.clean(err.backtrace)||[]
      if _backtrace.size>0
        opts = opts.merge ({
                              :full_message => _backtrace.join("\n"),
                              :file => _backtrace[0].split(":")[0],
                              :line => _backtrace[0].split(":")[1],
                          })
      end
    end

    unless env.nil?
      request = ActionDispatch::Request.new(env)

      request_items = {:url => request.original_url,
                       :http_method => request.method,
                       :ip_address => request.remote_ip,
                       :parameters => request.filtered_parameters,
                       :timestamp => Time.current}

      opts[:request] = request_items
      opts[:session] = request.session
      opts[:cookies] = request.cookies
      # opts[:environment] = request.filtered_env
    end
    @notifier.notify!(opts)
  rescue Exception => i_err
    puts "Graylog2 Exception logger. Could not send message: " + i_err.message
  end
end