grape-middleware-lograge 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0749b704c2396f878768bc0b2bc8680a2bc1680b
4
- data.tar.gz: b9bdf6e794fd12569c935bb41082d8ace78f44e0
3
+ metadata.gz: 67d76f3667aeef4f551393982de8b570bae58e6b
4
+ data.tar.gz: 3e4768bba774e30e2f7269ce4edd0852b32e60f7
5
5
  SHA512:
6
- metadata.gz: b23a158180609730034d9764f504caef4153c578a52e58e039f851ae6866309000ed1bfc8cd83be728f161cc67e82de2e8f26d4e0a2f7d9f08821ec867ef961b
7
- data.tar.gz: 1d71a59f35cab62b41ef938891879eeb32d0570793a53823bed4091492c584ee3b86cafcbc71cf0f6b69fa87d9333dc70f640c295712302118cbae324d7b574f
6
+ metadata.gz: 2f13ecd79731062f3ec8a472ceb1d78f0fa4718a4994472f00dc31a894ba291e7b2dfbec053915f02b672f862f8cae38cc33ad61954dcdab536a636aba9a31f4
7
+ data.tar.gz: 89c747b20d031d1f3de14780dab998e4e79f185d121979bd3574d8f7c90c73e888752eaa1e0453aa5309787363194a4680fa7694f14628856b89f64aa5051ee2
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.2.1
5
4
  - 2.2.2
6
5
  - 2.2.3
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'grape-middleware-lograge'
5
- spec.version = '1.1.0'
5
+ spec.version = '1.2.0'
6
6
  spec.platform = Gem::Platform::RUBY
7
7
  spec.authors = ['Ryan Buckley', 'Paul Chavard']
8
8
  spec.email = ['arebuckley@gmail.com', 'paul+github@chavard.net']
@@ -1,4 +1,6 @@
1
1
  require 'grape'
2
+ require 'lograge'
3
+ require 'lograge/formatters/rails_logger'
2
4
 
3
5
  class Grape::Middleware::Lograge < Grape::Middleware::Globals
4
6
  BACKSLASH = '/'.freeze
@@ -9,6 +11,18 @@ class Grape::Middleware::Lograge < Grape::Middleware::Globals
9
11
 
10
12
  class << self
11
13
  attr_accessor :filter
14
+
15
+ def custom_options
16
+ -> (event) {
17
+ {
18
+ params: event.payload[:params],
19
+ user_agent: event.payload[:user_agent],
20
+ request_id: event.payload[:request_id],
21
+ remote_ip: event.payload[:remote_ip],
22
+ version: event.payload[:version]
23
+ }
24
+ }
25
+ end
12
26
  end
13
27
 
14
28
  def initialize(_, options = {})
@@ -19,6 +33,13 @@ class Grape::Middleware::Lograge < Grape::Middleware::Globals
19
33
  def before
20
34
  super
21
35
 
36
+ @db_duration = 0
37
+
38
+ @db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
39
+ event = ActiveSupport::Notifications::Event.new(*args)
40
+ @db_duration += event.duration
41
+ end if defined?(ActiveRecord)
42
+
22
43
  ActiveSupport::Notifications.instrument("start_processing.grape", raw_payload)
23
44
  end
24
45
 
@@ -54,12 +75,15 @@ class Grape::Middleware::Lograge < Grape::Middleware::Globals
54
75
  end
55
76
 
56
77
  def after(payload, status)
57
- payload[:status] = status
58
- payload[:format] = env['api.format']
59
- payload[:version] = env['api.version']
78
+ payload[:status] = status
79
+ payload[:format] = env['api.format']
80
+ payload[:version] = env['api.version']
81
+ payload[:db_runtime] = @db_duration
60
82
  end
61
83
 
62
84
  def after_exception(payload, e)
85
+ ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription
86
+
63
87
  class_name = e.class.name
64
88
  status = e.respond_to?(:status) ? e.status : 500
65
89
 
@@ -70,6 +94,8 @@ class Grape::Middleware::Lograge < Grape::Middleware::Globals
70
94
  end
71
95
 
72
96
  def after_failure(payload, error)
97
+ ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription
98
+
73
99
  after(payload, error[:status])
74
100
  end
75
101
 
@@ -86,7 +112,7 @@ class Grape::Middleware::Lograge < Grape::Middleware::Globals
86
112
  def raw_payload
87
113
  {
88
114
  params: parameters.merge(
89
- 'action' => action_name || 'index',
115
+ 'action' => action_name.empty? ? 'index' : action_name,
90
116
  'controller' => controller
91
117
  ),
92
118
  method: env[Grape::Env::GRAPE_REQUEST].request_method,
@@ -2,7 +2,8 @@ class Grape::Middleware::Lograge::Railtie < Rails::Railtie
2
2
  initializer 'grape.middleware.lograge', after: :load_config_initializers do
3
3
  Grape::Middleware::Lograge.filter = ActionDispatch::Http::ParameterFilter.new Rails.application.config.filter_parameters
4
4
 
5
- require 'lograge'
5
+ Rails.application.config.lograge.custom_options = Grape::Middleware::Lograge.custom_options
6
+
6
7
  ::Lograge::RequestLogSubscriber.attach_to :grape
7
8
  end
8
9
  end
@@ -39,7 +39,15 @@ module Lograge
39
39
  if data[:error]
40
40
  "Error #{status} #{data[:error]} after #{duration}ms"
41
41
  else
42
- "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{duration}ms"
42
+ lines = []
43
+ additions = []
44
+
45
+ additions << "ActiveRecord: %.1fms" % data[:db].to_f
46
+
47
+ lines << "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{duration}ms"
48
+ lines << " (#{additions.join(" | ")})" unless additions.blank?
49
+
50
+ lines.join("\n")
43
51
  end
44
52
  end
45
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-middleware-lograge
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley