grape_logging 1.6.0 → 1.7.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: abce5627a8773555c3f05b4ffdc839fd051a3bf9
4
- data.tar.gz: f28eb40f63b48bd004606238606486520de93223
3
+ metadata.gz: a1ffd8bd8e43e3c62b4ea629ff034db37f60f4e5
4
+ data.tar.gz: e57a2e7ee7937252d33c921ea97d8c820233889d
5
5
  SHA512:
6
- metadata.gz: 0d36b9d5a70622758306bfbc26277c1b314643f1b19e7e3badac1abf098aa80f1566745aab9baf467a90345154ec0386bfacd8b162fc9e578e4177f878bf1b9a
7
- data.tar.gz: e9dbc09c6c799b23e336195179940da48a9fc7ee532aa158d3c6530033b92bb5d676194f3c6a99ecb7f3de0293a3f2a2fdf4682ec312ee84ed20747a946483f3
6
+ metadata.gz: 79619111ac1fe3148fb631770e88da6e3b74f06447f15969194776dc7926107e3b32d1ae9feaa1c61b62e406771e9aece3c6a7d17e83d0a4c21f0d29e666318d
7
+ data.tar.gz: 8d3777d6d64f1450de33795f6aebaddce650afb7d5d52fc2907a1090a268a38d38ba5c826417bd252289a9fc7b35be29a685500b6d6b252a857bd31778130529
@@ -7,7 +7,8 @@ module GrapeLogging
7
7
  severity: severity,
8
8
  duration: time[:total],
9
9
  db: time[:db],
10
- view: time[:view]
10
+ view: time[:view],
11
+ datetime: datetime.iso8601
11
12
  }.merge(data)
12
13
  ::Lograge.formatter.call(attributes) + "\n"
13
14
  end
@@ -6,14 +6,22 @@ module GrapeLogging
6
6
  end
7
7
 
8
8
  private
9
+
9
10
  # In some cases, response.body is not parseable by JSON.
10
11
  # For example, if you POST on a PUT endpoint, response.body is egal to """".
11
12
  # It's strange but it's the Grape behavior...
12
13
  def serialized_response_body(response)
13
- begin
14
- response.body.map{ |body| JSON.parse(body.to_s) }
15
- rescue => e
16
- response.body
14
+
15
+ if response.respond_to?(:body)
16
+ # Rack responses
17
+ begin
18
+ response.body.map{ |body| JSON.parse(body.to_s) }
19
+ rescue # No reason to have "=> e" here when we don't use it..
20
+ response.body
21
+ end
22
+ else
23
+ # Error & Exception responses
24
+ response
17
25
  end
18
26
  end
19
27
  end
@@ -9,6 +9,10 @@ module GrapeLogging
9
9
  GrapeLogging::Timings.append_db_runtime(event)
10
10
  end if defined?(ActiveRecord)
11
11
 
12
+ # Persist response status & response (body)
13
+ # to use int in parameters
14
+ attr_accessor :response_status, :response_body
15
+
12
16
  def initialize(app, options = {})
13
17
  super
14
18
 
@@ -23,34 +27,71 @@ module GrapeLogging
23
27
  def before
24
28
  reset_db_runtime
25
29
  start_time
26
-
27
30
  invoke_included_loggers(:before)
28
31
  end
29
32
 
30
- def after
33
+ def after(status, response)
31
34
  stop_time
35
+
36
+ # Response status
37
+ @response_status = status
38
+ @response_body = response
39
+
40
+ # Perform repotters
32
41
  @reporter.perform(collect_parameters)
42
+
43
+ # Invoke loggers
33
44
  invoke_included_loggers(:after)
34
45
  nil
35
46
  end
36
47
 
48
+ # Call stack and parse responses & status.
49
+ #
50
+ # @note Exceptions are logged as 500 status & re-raised.
37
51
  def call!(env)
38
- super
39
- end
52
+ @env = env
40
53
 
41
- protected
54
+ # Before hook
55
+ before
42
56
 
43
- def response
44
- begin
45
- super
46
- rescue
57
+ # Catch error
58
+ error = catch(:error) do
59
+ begin
60
+ @app_response = @app.call(@env)
61
+ rescue => e
62
+ # Log as 500 + message
63
+ after(e.respond_to?(:status) ? e.status : 500, e.message)
64
+
65
+ # Re-raise exception
66
+ raise e
67
+ end
47
68
  nil
48
69
  end
70
+
71
+ # Get status & response from app_response
72
+ # when no error occures.
73
+ if error
74
+ # Call with error & response
75
+ after(error[:status], error[:message])
76
+
77
+ # Throw again
78
+ throw(:error, error)
79
+ else
80
+ status, _, resp = *@app_response
81
+
82
+ # Call after hook properly
83
+ after(status, resp)
84
+ end
85
+
86
+ # Otherwise return original response
87
+ @app_response
49
88
  end
50
89
 
90
+ protected
91
+
51
92
  def parameters
52
93
  {
53
- status: response.nil? ? 'fail' : response.status,
94
+ status: response_status,
54
95
  time: {
55
96
  total: total_runtime,
56
97
  db: db_runtime,
@@ -64,8 +105,9 @@ module GrapeLogging
64
105
  end
65
106
 
66
107
  private
108
+
67
109
  def request
68
- @request ||= ::Rack::Request.new(env)
110
+ @request ||= ::Rack::Request.new(@env)
69
111
  end
70
112
 
71
113
  def total_runtime
@@ -95,7 +137,7 @@ module GrapeLogging
95
137
  def collect_parameters
96
138
  parameters.tap do |params|
97
139
  @included_loggers.each do |logger|
98
- params.merge! logger.parameters(request, response) do |_, oldval, newval|
140
+ params.merge! logger.parameters(request, response_body) do |_, oldval, newval|
99
141
  oldval.respond_to?(:merge) ? oldval.merge(newval) : newval
100
142
  end
101
143
  end
@@ -1,3 +1,3 @@
1
1
  module GrapeLogging
2
- VERSION = '1.6.0'
2
+ VERSION = '1.7.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.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aserafin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-01 00:00:00.000000000 Z
11
+ date: 2017-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape