loggerator 0.0.1 → 0.0.2
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 +4 -4
- data/lib/generators/templates/log.rb.erb +1 -1
- data/lib/loggerator/loggerator.rb +71 -67
- data/lib/loggerator/metrics.rb +0 -1
- data/lib/loggerator/namespace.rb +13 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 147fb6d7ba2b85f8916e9b8e5e96ac088baa8fa2
|
4
|
+
data.tar.gz: 699170130066ddbc05ba5ba8f33e1bfccfc2ddda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e85021a1d1bc361b85a764e8ec65c9ec097eeee232f77ff05cc7ad1f3850afef012dee300b16b2cbae99e9a1aca90edeb03c810b424dd0e27ddea785cd298daf
|
7
|
+
data.tar.gz: 61852b4aada68c3c5c931876d310a777092ee3809fcc424ae7f555667345e8149065548d2fe1f358e0daf0f3d8170af7320c56f346a8e5d2340ab7da05776bc6
|
@@ -2,23 +2,22 @@ require_relative 'request_store'
|
|
2
2
|
require_relative 'middleware'
|
3
3
|
|
4
4
|
module Loggerator
|
5
|
-
extend self
|
6
5
|
|
7
6
|
def self.included(mod)
|
8
7
|
mod.extend self
|
9
8
|
end
|
10
9
|
|
11
10
|
def log(data, &block)
|
12
|
-
|
11
|
+
Log.to_stream(Log.stdout, Log.contexts(data), &block)
|
13
12
|
end
|
14
13
|
|
15
|
-
def log_error(e
|
14
|
+
def log_error(e=$!, data = {})
|
16
15
|
exception_id = e.object_id
|
17
16
|
|
18
17
|
# Log backtrace in reverse order for easier digestion.
|
19
18
|
if e.backtrace
|
20
19
|
e.backtrace.reverse.each do |backtrace|
|
21
|
-
|
20
|
+
Log.to_stream(Log.stderr, Log.contexts(
|
22
21
|
exception_id: exception_id,
|
23
22
|
backtrace: backtrace
|
24
23
|
))
|
@@ -36,45 +35,28 @@ module Loggerator
|
|
36
35
|
|
37
36
|
data[:status] = e.status if e.respond_to?(:status)
|
38
37
|
|
39
|
-
|
38
|
+
Log.to_stream(Log.stderr, Log.contexts(data))
|
40
39
|
end
|
41
40
|
|
42
41
|
def log_context(data, &block)
|
43
|
-
old = local_context
|
44
|
-
|
42
|
+
old = Log.local_context
|
43
|
+
Log.local_context = old.merge(data)
|
45
44
|
res = block.call
|
46
45
|
ensure
|
47
|
-
|
46
|
+
Log.local_context = old
|
48
47
|
res
|
49
48
|
end
|
50
49
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def default_context
|
56
|
-
@@default_context ||= {}
|
57
|
-
end
|
58
|
-
|
59
|
-
def stdout=(stream)
|
60
|
-
@@stdout = stream
|
61
|
-
end
|
62
|
-
|
63
|
-
def stdout
|
64
|
-
@@stdout ||= $stdout
|
65
|
-
end
|
66
|
-
|
67
|
-
def stderr=(stream)
|
68
|
-
@@stderr = stream
|
69
|
-
end
|
50
|
+
# Don't expose internals into included modules so name-collisions are reduced
|
51
|
+
module Log
|
52
|
+
extend self
|
70
53
|
|
71
|
-
|
72
|
-
|
73
|
-
|
54
|
+
def default_context=(default_context)
|
55
|
+
@@default_context = default_context
|
56
|
+
end
|
74
57
|
|
75
|
-
|
76
|
-
|
77
|
-
default_context.merge(request_context.merge(local_context.merge(data)))
|
58
|
+
def default_context
|
59
|
+
@@default_context ||= {}
|
78
60
|
end
|
79
61
|
|
80
62
|
def local_context
|
@@ -85,62 +67,84 @@ module Loggerator
|
|
85
67
|
RequestStore.store[:local_context] = h
|
86
68
|
end
|
87
69
|
|
88
|
-
def
|
89
|
-
|
70
|
+
def stdout=(stream)
|
71
|
+
@@stdout = stream
|
72
|
+
end
|
73
|
+
|
74
|
+
def stdout
|
75
|
+
@@stdout ||= $stdout
|
76
|
+
end
|
77
|
+
|
78
|
+
def stderr=(stream)
|
79
|
+
@@stderr = stream
|
90
80
|
end
|
91
81
|
|
92
|
-
def
|
82
|
+
def stderr
|
83
|
+
@@stderr ||= $stderr
|
84
|
+
end
|
85
|
+
|
86
|
+
def contexts(data)
|
87
|
+
default_context.merge(request_context.merge(local_context.merge(data)))
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_stream(stream, data, &block)
|
93
91
|
unless block
|
94
92
|
str = unparse(data)
|
95
93
|
stream.print(str + "\n")
|
96
94
|
else
|
97
95
|
data = data.dup
|
98
96
|
start = Time.now
|
99
|
-
|
97
|
+
to_stream(stream, data.merge(at: 'start'))
|
100
98
|
begin
|
101
99
|
res = yield
|
102
100
|
|
103
|
-
|
101
|
+
to_stream(stream, data.merge(
|
104
102
|
at: 'finish', elapsed: (Time.now - start).to_f))
|
105
103
|
res
|
106
104
|
rescue
|
107
|
-
|
105
|
+
to_stream(stream, data.merge(
|
108
106
|
at: 'exception', elapsed: (Time.now - start).to_f))
|
109
107
|
raise $!
|
110
108
|
end
|
111
109
|
end
|
112
110
|
end
|
113
111
|
|
114
|
-
|
115
|
-
|
116
|
-
|
112
|
+
private
|
113
|
+
def request_context
|
114
|
+
RequestStore.store[:request_context] || {}
|
115
|
+
end
|
117
116
|
|
118
|
-
|
119
|
-
|
120
|
-
# only quote strings if they include whitespace
|
121
|
-
if v == nil
|
122
|
-
nil
|
123
|
-
elsif v == true
|
124
|
-
k
|
125
|
-
elsif v.is_a?(Float)
|
126
|
-
"#{k}=#{format("%.3f", v)}"
|
127
|
-
elsif v.is_a?(String) && v =~ /\s/
|
128
|
-
quote_string(k, v)
|
129
|
-
elsif v.is_a?(Time)
|
130
|
-
"#{k}=#{v.iso8601}"
|
131
|
-
else
|
132
|
-
"#{k}=#{v}"
|
117
|
+
def unparse(attrs)
|
118
|
+
attrs.map { |k, v| unparse_pair(k, v) }.compact.join(" ")
|
133
119
|
end
|
134
|
-
end
|
135
120
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
121
|
+
def unparse_pair(k, v)
|
122
|
+
v = v.call if v.is_a?(Proc)
|
123
|
+
# only quote strings if they include whitespace
|
124
|
+
if v == nil
|
125
|
+
nil
|
126
|
+
elsif v == true
|
127
|
+
k
|
128
|
+
elsif v.is_a?(Float)
|
129
|
+
"#{k}=#{format("%.3f", v)}"
|
130
|
+
elsif v.is_a?(String) && v =~ /\s/
|
131
|
+
quote_string(k, v)
|
132
|
+
elsif v.is_a?(Time)
|
133
|
+
"#{k}=#{v.iso8601}"
|
134
|
+
else
|
135
|
+
"#{k}=#{v}"
|
136
|
+
end
|
144
137
|
end
|
145
|
-
|
138
|
+
|
139
|
+
def quote_string(k, v)
|
140
|
+
# try to find a quote style that fits
|
141
|
+
if !v.include?('"')
|
142
|
+
%{#{k}="#{v}"}
|
143
|
+
elsif !v.include?("'")
|
144
|
+
%{#{k}='#{v}'}
|
145
|
+
else
|
146
|
+
%{#{k}="#{v.gsub(/"/, '\\"')}"}
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
146
150
|
end
|
data/lib/loggerator/metrics.rb
CHANGED
data/lib/loggerator/namespace.rb
CHANGED
@@ -4,19 +4,25 @@ module Loggerator
|
|
4
4
|
module Namespace
|
5
5
|
include Loggerator
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
7
|
+
def self.included(mod)
|
8
|
+
mod.extend self
|
9
|
+
end
|
10
|
+
|
11
|
+
def log(data={}, &block)
|
12
|
+
log_namespace! do
|
13
|
+
super
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
def log_error(e, data={})
|
13
|
-
log_namespace!
|
14
|
-
|
18
|
+
log_namespace! do
|
19
|
+
super
|
20
|
+
end
|
15
21
|
end
|
16
22
|
|
17
23
|
private
|
18
|
-
def log_namespace!
|
19
|
-
|
24
|
+
def log_namespace!(&block)
|
25
|
+
log_context({ns: kind_of?(Module) ? name : self.class.name }, &block)
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loggerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Mervine
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-10-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple web application extension for logging, following the 12factor
|
15
15
|
pattern.
|