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



53
54
55
# File 'lib/thrift_rack/logger.rb', line 53

def tag
  @tag ||= {}
end

Class Method Details

.loggerObject



45
46
47
48
49
50
51
# File 'lib/thrift_rack/logger.rb', line 45

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
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/thrift_rack/logger.rb', line 9

def call(env)
  request_at = env['LAUNCH_AT'] || Time.now
  income_middleware_duration = Time.now - request_at
  req = Rack::Request.new(env)
  resp = @app.call(env)
  resp
ensure
  duration = ((Time.now - request_at) * 1000).round(4)
  request_id = req.env["HTTP_X_REQUEST_ID"]
  if req.env["HTTP_X_FULL_TRACE"]
    full_trace = req.env["HTTP_X_FULL_TRACE"] == "true"
  else
    full_trace = request_id.hash % 8 == 0
  end
  if full_trace || duration >= 100
    ThriftRack::Logger.logger.info(
      JSON.dump(
        request_at: request_at.iso8601(6),
        request_id: request_id,
        rpc_id: req.env["HTTP_X_RPC_ID"],
        duration: duration,
        income_middleware_duration: (income_middleware_duration * 1000).round(2),
        atom_duration: env["ATOM_DURATION"],
        path: req.path,
        func: req.env["HTTP_X_RPC_FUNC"],
        from: req.env["HTTP_X_FROM"],
        full_trace: full_trace,
        tag: Logger.tag,
      ),
    )
  end
end