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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b09044e8590344b2433a544b15a853d63e92d06b
4
- data.tar.gz: 1fc9edb37658480a85d314177440e4dd79d568c8
3
+ metadata.gz: 147fb6d7ba2b85f8916e9b8e5e96ac088baa8fa2
4
+ data.tar.gz: 699170130066ddbc05ba5ba8f33e1bfccfc2ddda
5
5
  SHA512:
6
- metadata.gz: baf7c00883dcfec5e32f33feeabb28d9ecf541a0cfe3c72f503b62740d18d38f2465c71dd676da26d7056c33acad54eac304e5274a61f731037787d8f59237e2
7
- data.tar.gz: f5912098321f92a9d449e3029a53cc3c33609531e5c0ea5faf02ee6f8793bb75a5fc742689056fe6ec6c247371e82d7149b26b304185de5fca2662d96079d401
6
+ metadata.gz: e85021a1d1bc361b85a764e8ec65c9ec097eeee232f77ff05cc7ad1f3850afef012dee300b16b2cbae99e9a1aca90edeb03c810b424dd0e27ddea785cd298daf
7
+ data.tar.gz: 61852b4aada68c3c5c931876d310a777092ee3809fcc424ae7f555667345e8149065548d2fe1f358e0daf0f3d8170af7320c56f346a8e5d2340ab7da05776bc6
@@ -1,4 +1,4 @@
1
- Loggerator.default_context = { app: "<%= app_name %>" }
1
+ Loggerator::Log.default_context = { app: "<%= app_name %>" }
2
2
  <% if defined?(Loggerator::Metrics) -%>
3
3
  Loggerator::Metrics.name = "<%= app_name %>"
4
4
  <% end -%>
@@ -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
- log_to_stream(stdout, merge_log_contexts(data), &block)
11
+ Log.to_stream(Log.stdout, Log.contexts(data), &block)
13
12
  end
14
13
 
15
- def log_error(e, data = {})
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
- log_to_stream(stderr, merge_log_contexts(
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
- log_to_stream(stderr, merge_log_contexts(data))
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
- self.local_context = old.merge(data)
42
+ old = Log.local_context
43
+ Log.local_context = old.merge(data)
45
44
  res = block.call
46
45
  ensure
47
- self.local_context = old
46
+ Log.local_context = old
48
47
  res
49
48
  end
50
49
 
51
- def default_context=(default_context)
52
- @@default_context = default_context
53
- end
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
- def stderr
72
- @@stderr ||= $stderr
73
- end
54
+ def default_context=(default_context)
55
+ @@default_context = default_context
56
+ end
74
57
 
75
- private
76
- def merge_log_contexts(data)
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 request_context
89
- RequestStore.store[:request_context] || {}
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 log_to_stream(stream, data, &block)
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
- log_to_stream(stream, data.merge(at: 'start'))
97
+ to_stream(stream, data.merge(at: 'start'))
100
98
  begin
101
99
  res = yield
102
100
 
103
- log_to_stream(stream, data.merge(
101
+ to_stream(stream, data.merge(
104
102
  at: 'finish', elapsed: (Time.now - start).to_f))
105
103
  res
106
104
  rescue
107
- log_to_stream(stream, data.merge(
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
- def unparse(attrs)
115
- attrs.map { |k, v| unparse_pair(k, v) }.compact.join(" ")
116
- end
112
+ private
113
+ def request_context
114
+ RequestStore.store[:request_context] || {}
115
+ end
117
116
 
118
- def unparse_pair(k, v)
119
- v = v.call if v.is_a?(Proc)
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
- def quote_string(k, v)
137
- # try to find a quote style that fits
138
- if !v.include?('"')
139
- %{#{k}="#{v}"}
140
- elsif !v.include?("'")
141
- %{#{k}='#{v}'}
142
- else
143
- %{#{k}="#{v.gsub(/"/, '\\"')}"}
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
- end
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
@@ -30,7 +30,6 @@ module Loggerator
30
30
  def measure(key, value, units='s')
31
31
  log("measure##{name}.#{key}" => "#{value}#{units}")
32
32
  end
33
-
34
33
  end
35
34
 
36
35
  # included Metrics shortcut
@@ -4,19 +4,25 @@ module Loggerator
4
4
  module Namespace
5
5
  include Loggerator
6
6
 
7
- def log(data={}, &blk)
8
- log_namespace!
9
- super
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
- super
18
+ log_namespace! do
19
+ super
20
+ end
15
21
  end
16
22
 
17
23
  private
18
- def log_namespace!
19
- self.local_context = { ns: self.class.name }
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.1
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-08-23 00:00:00.000000000 Z
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.