onelinejson 0.0.2 → 0.0.3
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.
- data/lib/onelinejson.rb +15 -97
- data/lib/onelinejson/version.rb +1 -1
- metadata +2 -2
data/lib/onelinejson.rb
CHANGED
@@ -6,95 +6,6 @@ require 'active_support/core_ext/class/attribute'
|
|
6
6
|
require 'active_support/log_subscriber'
|
7
7
|
|
8
8
|
module Onelinejson
|
9
|
-
class TestSubscriber < ActiveSupport::LogSubscriber
|
10
|
-
def process_action(event)
|
11
|
-
return if ::Lograge.ignore?(event)
|
12
|
-
|
13
|
-
data = {
|
14
|
-
request: {},
|
15
|
-
response: {},
|
16
|
-
debug_info: {}
|
17
|
-
}
|
18
|
-
payload = event.payload
|
19
|
-
|
20
|
-
data[:request].merge! extract_request(payload)
|
21
|
-
data[:response].merge! extract_status(payload)
|
22
|
-
data[:response].merge! runtimes(event)
|
23
|
-
data[:response].merge! location(event)
|
24
|
-
data.merge! custom_options(event)
|
25
|
-
|
26
|
-
formatted_message = ::Lograge.formatter.call(data)
|
27
|
-
logger.send(::Lograge.log_level, formatted_message)
|
28
|
-
end
|
29
|
-
|
30
|
-
def redirect_to(event)
|
31
|
-
Thread.current[:lograge_location] = event.payload[:location]
|
32
|
-
end
|
33
|
-
|
34
|
-
def logger
|
35
|
-
::Lograge.logger.presence or super
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def extract_request(payload)
|
41
|
-
Hash[{
|
42
|
-
:method => payload[:method],
|
43
|
-
:path => extract_path(payload),
|
44
|
-
:format => extract_format(payload),
|
45
|
-
:controller => payload[:params]['controller'],
|
46
|
-
:action => payload[:params]['action'],
|
47
|
-
}.merge(payload[:request]).sort]
|
48
|
-
end
|
49
|
-
|
50
|
-
def extract_path(payload)
|
51
|
-
payload[:path].split("?").first
|
52
|
-
end
|
53
|
-
|
54
|
-
def extract_format(payload)
|
55
|
-
if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
|
56
|
-
payload[:formats].first
|
57
|
-
else
|
58
|
-
payload[:format]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def extract_status(payload)
|
63
|
-
if payload[:status]
|
64
|
-
{ :status => payload[:status].to_i }
|
65
|
-
elsif payload[:exception]
|
66
|
-
exception, message = payload[:exception]
|
67
|
-
{ :status => 500, :error => "#{exception}:#{message}" }
|
68
|
-
else
|
69
|
-
{ :status => 0 }
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def custom_options(event)
|
74
|
-
::Lograge.custom_options(event) || {}
|
75
|
-
end
|
76
|
-
|
77
|
-
def runtimes(event)
|
78
|
-
{
|
79
|
-
:duration => event.duration,
|
80
|
-
:view => event.payload[:view_runtime],
|
81
|
-
:db => event.payload[:db_runtime]
|
82
|
-
}.inject({}) do |runtimes, (name, runtime)|
|
83
|
-
runtimes[name] = runtime.to_f.round(2) if runtime
|
84
|
-
runtimes
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def location(event)
|
89
|
-
if location = Thread.current[:lograge_location]
|
90
|
-
Thread.current[:lograge_location] = nil
|
91
|
-
{ :location => location }
|
92
|
-
else
|
93
|
-
{}
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
9
|
module AppControllerMethods
|
99
10
|
def append_info_to_payload(payload)
|
100
11
|
super
|
@@ -115,21 +26,28 @@ module Onelinejson
|
|
115
26
|
date: Time.now.utc.iso8601,
|
116
27
|
}
|
117
28
|
payload[:request][:user_id] = current_user.id if defined?(current_user) && current_user
|
118
|
-
end
|
119
|
-
end
|
120
29
|
|
121
|
-
class JsonFormatter
|
122
|
-
def call(data)
|
123
|
-
::JSON.dump(data)
|
124
30
|
end
|
125
31
|
end
|
126
32
|
|
127
33
|
class Railtie < Rails::Railtie
|
128
34
|
config.lograge = ActiveSupport::OrderedOptions.new
|
129
|
-
config.lograge.
|
130
|
-
config.lograge.formatter = JsonFormatter.new
|
35
|
+
config.lograge.formatter = ::Lograge::Formatters::Json.new
|
131
36
|
config.lograge.enabled = true
|
132
|
-
|
37
|
+
config.lograge.before_format = lambda do |data, payload|
|
38
|
+
data.merge(payload)
|
39
|
+
request = payload[:request].merge(data.select{ |k,_|
|
40
|
+
[:method, :path, :format, :controller, :action].include?(k)
|
41
|
+
})
|
42
|
+
response = data.select{ |k,_|
|
43
|
+
[:status, :duration, :view, :view_runtime].include?(k)
|
44
|
+
}
|
45
|
+
Hash[{
|
46
|
+
request: request,
|
47
|
+
response: response,
|
48
|
+
debug_info: payload[:debug_info]
|
49
|
+
}.sort]
|
50
|
+
end
|
133
51
|
ActiveSupport.on_load(:action_controller) do
|
134
52
|
include AppControllerMethods
|
135
53
|
end
|
data/lib/onelinejson/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onelinejson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: lograge
|