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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1ffd8bd8e43e3c62b4ea629ff034db37f60f4e5
|
4
|
+
data.tar.gz: e57a2e7ee7937252d33c921ea97d8c820233889d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79619111ac1fe3148fb631770e88da6e3b74f06447f15969194776dc7926107e3b32d1ae9feaa1c61b62e406771e9aece3c6a7d17e83d0a4c21f0d29e666318d
|
7
|
+
data.tar.gz: 8d3777d6d64f1450de33795f6aebaddce650afb7d5d52fc2907a1090a268a38d38ba5c826417bd252289a9fc7b35be29a685500b6d6b252a857bd31778130529
|
@@ -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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
39
|
-
end
|
52
|
+
@env = env
|
40
53
|
|
41
|
-
|
54
|
+
# Before hook
|
55
|
+
before
|
42
56
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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:
|
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,
|
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
|
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.
|
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-
|
11
|
+
date: 2017-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|