grape_logging 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fcf09ce48949b6fa9b08da15da8aad3bc97cad72
4
- data.tar.gz: 5d4206727f908558817d5a8ea41deb4da85f4f25
3
+ metadata.gz: d214c8c6a482c7b66608429000a8b160e52d896b
4
+ data.tar.gz: 5c2fa4b0588255709302386f2e5f6070f6b5ad45
5
5
  SHA512:
6
- metadata.gz: 93a61ce5749bfb9bbb99e2bd1f15701432426f959da03d2b0203d2a12dd74222538b80794b61b39ffb886c2eb061f64b8817a757793ea9265b75f8b05baf57f3
7
- data.tar.gz: ab8caacfa27adaab5c0a635e4533cf1de2f349e1a33a9064ae7bbb5fadfb3c16d4f327a0c500977cf3189ea693c8d0cce487dac15e166ef327d814865e0a1637
6
+ metadata.gz: d692b98646ecf1ae6a5008625ab711737c8ceaa10b91064ac855df009c724350f757d83672a83d43eb826c8b232c2a64882e124e66071950b9f8f3ed359a8fe9
7
+ data.tar.gz: 0ac0c4faf27ca3318494f97a8535e7d8a0d174fa053bb6003542ccd702108e59553a5a59932858779f4f6fc044ec3a3c47f92c019ed507af9077d2f0aa64dcdc
@@ -1,38 +1,35 @@
1
- require 'benchmark'
2
1
  require 'grape/middleware/base'
3
2
 
4
3
  module GrapeLogging
5
4
  module Middleware
6
5
  class RequestLogger < Grape::Middleware::Base
7
6
  def before
8
- env[:db_duration] = 0
7
+ start_time
9
8
 
9
+ @db_duration = 0
10
10
  ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
11
11
  event = ActiveSupport::Notifications::Event.new(*args)
12
- env[:db_duration] += event.duration
12
+ @db_duration += event.duration
13
13
  end if defined?(ActiveRecord)
14
14
  end
15
15
 
16
- def call!(env)
17
- original_response = nil
18
-
19
- duration = Benchmark.realtime { original_response = super(env) }
20
- logger.info parameters(original_response, duration)
21
-
22
- original_response
16
+ def after
17
+ stop_time
18
+ logger.info parameters(request, response)
19
+ nil
23
20
  end
24
21
 
25
22
  protected
26
- def parameters(response, duration)
27
- {
28
- path: request.path,
29
- params: request.params,
30
- method: request.request_method,
31
- total: (duration * 1000).round(2),
32
- db: request.env[:db_duration].round(2),
33
- status: response.first
34
- }
35
- end
23
+ def parameters(request, response)
24
+ {
25
+ path: request.path,
26
+ params: request.params.to_hash,
27
+ method: request.request_method,
28
+ total: total_runtime,
29
+ db: @db_duration.round(2),
30
+ status: response.status
31
+ }
32
+ end
36
33
 
37
34
  private
38
35
  def logger
@@ -42,6 +39,18 @@ module GrapeLogging
42
39
  def request
43
40
  @request ||= ::Rack::Request.new(env)
44
41
  end
42
+
43
+ def total_runtime
44
+ ((stop_time - start_time) * 1000).round(2)
45
+ end
46
+
47
+ def start_time
48
+ @start_time ||= Time.now
49
+ end
50
+
51
+ def stop_time
52
+ @stop_time ||= Time.now
53
+ end
45
54
  end
46
55
  end
47
56
  end
@@ -1,3 +1,3 @@
1
1
  module GrapeLogging
2
- VERSION = '1.0.3'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aserafin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-04 00:00:00.000000000 Z
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape