Sha256: 9e4d5b12390b5d9471df3884c5e355e754fec8c381c2937e5b9af2d0a42dc9d7

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module Syslog
  class Logger
    alias_method :log, :add
  end
end

module Logatron
  class Middleware
    def initialize(app)
      @app = app
    end
    def call(env)
      request = ActionDispatch::Request.new(env)
      if request.headers['X-Ascent-Log-Id']
        Logatron.msg_id = request.headers['X-Ascent-Log-Id']
      elsif env['rack.session'].nil? or env['rack.session']['warden.user.user.key'].nil?
        Logatron.msg_id = SecureRandom.uuid + '-id-anonymous'
      else
        Logatron.msg_id = SecureRandom.uuid + '-id-' + env['rack.session']['warden.user.user.key'][0][0].to_s
      end
      @app.call(env)
    end
  end

  class Railtie < Rails::Railtie

    initializer 'logatron.configure_rails_initialization' do |app|

      if defined?(Warden::Manager)
        app.middleware.insert_before(Warden::Manager, Logatron::Middleware)
      else
        app.middleware.use Logatron::Middleware
      end

    end

      config.lograge.logger = Logatron.logger
      config.lograge.enabled = true
      config.lograge.formatter = Lograge::Formatters::Json.new
      config.lograge.custom_options = lambda do |event|
        {:source => event.payload[:ip], :severity=> Logatron::INFO, :site => Logatron.site, :timestamp => Time.now.iso8601, :host => Logatron.configuration.host, :id => Logatron.msg_id}
      end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logatron-0.2.0 lib/logatron/railtie.rb
logatron-0.1.11 lib/logatron/railtie.rb