Class: ThriftRack::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_rack/logger.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Logger

Returns a new instance of Logger.



5
6
7
# File 'lib/thrift_rack/logger.rb', line 5

def initialize(app)
  @app = app
end

Class Attribute Details

.tagObject



41
42
43
# File 'lib/thrift_rack/logger.rb', line 41

def tag
  @tag ||= {}
end

Class Method Details

.loggerObject



33
34
35
36
37
38
39
# File 'lib/thrift_rack/logger.rb', line 33

def logger
  @logger ||= if defined? Rails
                ActiveSupport::Logger.new(File.open("#{Rails.root}/log/rpc.log", File::WRONLY | File::APPEND | File::CREAT))
              else
                ::Logger.new(STDOUT)
              end
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/thrift_rack/logger.rb', line 9

def call(env)
  request_at = env['LAUNCH_AT'] || Time.now
  req = Rack::Request.new(env)
  resp = @app.call(env)
  resp
ensure
  end_time = Time.now
  ThriftRack::Logger.logger.info(
    JSON.dump(
      request_at: request_at.iso8601(6),
      request_id: req.env["HTTP_X_REQUEST_ID"],
      rpc_id: req.env["HTTP_X_RPC_ID"],
      duration: ((end_time - request_at) * 1000).round(4),
      path: req.path,
      func: req.env["HTTP_X_RPC_FUNC"],
      from: req.env["HTTP_X_FROM"],
      tag: Logger.tag,
    ),
  )
end