Class: Graylog2Rails::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/graylog2-rails/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/graylog2-rails/middleware.rb', line 5

def initialize(app, args = {})
  @app, @args = app, args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/graylog2-rails/middleware.rb', line 3

def args
  @args
end

Instance Method Details

#_call(env) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/graylog2-rails/middleware.rb', line 13

def _call(env)
  begin
    @app.call(env)
  rescue Exception => err
    send_to_graylog2(err, env)
    raise
  end
end

#call(env) ⇒ Object



9
10
11
# File 'lib/graylog2-rails/middleware.rb', line 9

def call(env)
  dup._call(env)
end

#send_to_graylog2(err, env) ⇒ Object



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
# File 'lib/graylog2-rails/middleware.rb', line 22

def send_to_graylog2 err, env
  opts = {:rack_env => env}
  begin
    opts = opts.merge(:exception => err) if err.is_a?(Exception)
    opts = opts.merge(err.to_hash) if err.respond_to?(:to_hash)
    notice = Graylog2Rails::Message.new(opts)
    args = {
      :short_message => notice.message,
      :full_message => notice.to_s,
      :facility => @args.delete("facility") + "_exception",
      :level => @args.delete("level"),
      :host => @args.delete("local_app_name"),
      :file => err.backtrace[0].split(":")[0],
      :line => err.backtrace[0].split(":")[1],
    }
    Rails.logger.tagged("GRAYLOG") do
      Rails.logger.info args.inspect
    end
    notifier = GELF::Notifier.new(@args.delete("hostname"), @args.delete("port"), @args.delete("max_chunk_size"))
    notifier.notify!(args)
  rescue => i_err
    puts "Graylog2 Exception logger. Could not send message: " + i_err.message
    puts i_err.backtrace.join("\n")
  end
end