Sha256: 1f089811e328a90bb14ea7a74e8b6eeb854ea02d5ea26848e388cf0938bc12ae

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Graylog2Rails
  class Middleware
    attr_reader :args

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

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

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

    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
  end # class Middleware
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graylog2-rails-0.0.1 lib/graylog2-rails/middleware.rb