lines 0.1.19 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjI0OWIzNjUxNDMzZjFiOGFjYjQ2NWY0YzBiMGFkNGRiNTkyM2Q1YQ==
4
+ NGU4YzQzOTg2NjJlNWFjNjA2ZDliYzEzYTRkMDE3OWJjZGRjZmI0NQ==
5
5
  data.tar.gz: !binary |-
6
- Y2EzOWYzNDlmZjkyMGRlZDJkNWFjNGU0NjYyNzU1YTZkYzUwYjlkNg==
6
+ NzIzODU1MzljMTg5N2I1MzVhMjdlN2EwOTE4YmVlY2ZlMjE4NDM4Yg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NGY3YTIwMmExZDM0OGI3OTQ4YTA3ZmJiODkyNzUzMzIxMTZiYmFmMWEyYmUy
10
- NWM4YTQ4M2VlNjc5ZDlmNmEwZDM2MTZkYWI0NDJiYjA3YjBhZTlhMGI1MDAx
11
- YzNkMmMwNmJiZGU0MTc4YjM0MzhkN2VkOGMzZDBiN2U1MmVkZjI=
9
+ YmQ0MGI3ODU4ZWJhNDM1NzA5ZDlmMWI2NjNiMmFjMDQ2YjZjZDExZjExMjk4
10
+ MDFlZWQyMmRmY2IxZjY4NDc1ODljZmI3NmYwZTkzZjBiYjUwZWY0OWI4MjU4
11
+ ZDE3NThhMDhhMWY5NTlkYWZhY2M3YjcwMjZhY2Q2MzlhZDNlNjE=
12
12
  data.tar.gz: !binary |-
13
- NTcwMTE4YmIxMmU2MTE3YTZhY2Q5NzNlMDg4NTBhODY5N2MxMTY2NWVjYzli
14
- NGNjMzE1ZWFmMDBjNGQ0NTE1NDFhMDY3YWI4MDQ3MDQ0MmMxY2ZjMTliMzU5
15
- ZTUxM2Y1OWQ5NmQ5MGQwZThmODNhOWFhYWQwYzkwYWEyODBjOTY=
13
+ OTg1YjllMzkyNjMzNmIwZjdmYTU2MDQzZmIzNmNmY2IyOTE5ZTJiNDkwYWRi
14
+ M2U1YWE5OWEzNjk5YjkzMmU2OWFlZDRjMWI1NzZmNjVjOWQwY2NkNmY3NmYx
15
+ MjIzNWFjMjNmNjllMGZlZDBlNTljNzMwZmE4OWUzYjMzZWIzYTQ=
@@ -0,0 +1 @@
1
+ Gemfile.lock
@@ -56,8 +56,7 @@ module Lines
56
56
  # obj - a ruby hash
57
57
  # args -
58
58
  def log(obj, args={})
59
- obj = sanitize_obj(obj, args)
60
- obj = global.merge(obj)
59
+ obj = prepare_obj(obj, args)
61
60
  outputters.each{|out| out.output(dumper, obj) }
62
61
  obj
63
62
  end
@@ -65,25 +64,15 @@ module Lines
65
64
  # Add data to the logs
66
65
  #
67
66
  # data - a ruby hash
67
+ #
68
+ # return a Context instance
68
69
  def context(data={})
69
- new_context = Context.new global.merge(data)
70
+ new_context = Context.new ensure_hash!(data)
70
71
  yield new_context if block_given?
71
72
  new_context
72
73
  end
73
74
 
74
- class Context
75
- attr_reader :data
76
-
77
- def initialize(data)
78
- @data = data
79
- end
80
-
81
- def log(obj, args={})
82
- Lines.log(obj, args.merge(data))
83
- end
84
- end
85
-
86
- # A backward-compatibile logger
75
+ # Returns a backward-compatibile logger
87
76
  def logger
88
77
  @logger ||= (
89
78
  require "lines/logger"
@@ -91,23 +80,34 @@ module Lines
91
80
  )
92
81
  end
93
82
 
83
+ def ensure_hash!(obj) # :nodoc:
84
+ return {} unless obj
85
+ return obj if obj.kind_of?(Hash)
86
+ return obj.to_h if obj.respond_to?(:to_h)
87
+ obj = {msg: obj}
88
+ end
89
+
94
90
  protected
95
91
 
96
- def sanitize_obj(obj, args={})
92
+ def prepare_obj(obj, args={})
97
93
  if obj.kind_of?(Exception)
98
94
  ex = obj
99
95
  obj = {ex: ex.class, msg: ex.to_s}
100
96
  if ex.respond_to?(:backtrace) && ex.backtrace
101
97
  obj[:backtrace] = ex.backtrace
102
98
  end
103
- elsif !obj.kind_of?(Hash)
104
- if obj.respond_to?(:to_h)
105
- obj = obj.to_h
106
- else
107
- obj = {msg: obj}
108
- end
99
+ else
100
+ obj = ensure_hash!(obj)
109
101
  end
110
- obj.merge(args)
102
+
103
+ args = ensure_hash!(args)
104
+
105
+ g = global.inject({}) do |h, (k,v)|
106
+ h[k] = v.respond_to?(:call) ? v.call : v
107
+ h
108
+ end
109
+
110
+ g.merge(obj.merge(args))
111
111
  end
112
112
 
113
113
  def to_outputter(out)
@@ -125,6 +125,19 @@ module Lines
125
125
  end
126
126
  end
127
127
 
128
+ # Wrapper object that holds a given context. Emitted by Lines.with
129
+ class Context
130
+ attr_reader :data
131
+
132
+ def initialize(data)
133
+ @data = data
134
+ end
135
+
136
+ def log(obj, args={})
137
+ Lines.log obj, Lines.ensure_hash!(args).merge(data)
138
+ end
139
+ end
140
+
128
141
  class StreamOutputter
129
142
  # stream must accept a #write(str) message
130
143
  def initialize(stream = $stderr)
@@ -157,9 +170,8 @@ module Lines
157
170
  }
158
171
 
159
172
  def initialize(syslog = Syslog, app_name=nil)
160
- @app_name = app_name
161
173
  @syslog = syslog
162
- prepare_syslog
174
+ prepare_syslog(app_name)
163
175
  end
164
176
 
165
177
  def output(dumper, obj)
@@ -176,10 +188,9 @@ module Lines
176
188
 
177
189
  protected
178
190
 
179
- attr_reader :app_name
180
191
  attr_reader :syslog
181
192
 
182
- def prepare_syslog
193
+ def prepare_syslog(app_name)
183
194
  unless syslog.opened?
184
195
  # Did you know ? app_name is detected by syslog if nil
185
196
  syslog.open(app_name,
@@ -1,3 +1,3 @@
1
1
  module Lines
2
- VERSION = "0.1.19"
2
+ VERSION = "0.1.20"
3
3
  end
@@ -6,59 +6,90 @@ describe Lines do
6
6
  let(:outputter) { StringIO.new }
7
7
  let(:output) { outputter.string }
8
8
  before do
9
+ Lines.global.replace({})
9
10
  Lines.use(outputter)
10
11
  end
11
12
 
12
- it "logs stuff" do
13
- Lines.log(foo: 'bar')
14
- expect(output).to eq('foo=bar' + Lines::NL)
15
- end
13
+ context ".log" do
14
+ it "logs stuff" do
15
+ Lines.log(foo: 'bar')
16
+ expect(output).to eq('foo=bar' + Lines::NL)
17
+ end
16
18
 
17
- it "supports a first msg argument" do
18
- Lines.log("this user is annoying", user: 'bob')
19
- expect(output).to eq("msg='this user is annoying' user=bob" + Lines::NL)
20
- end
19
+ it "supports a first msg argument" do
20
+ Lines.log("this user is annoying", user: 'bob')
21
+ expect(output).to eq("msg='this user is annoying' user=bob" + Lines::NL)
22
+ end
21
23
 
22
- it "logs exceptions" do
23
- Lines.log(StandardError.new("error time!"), user: 'bob')
24
- expect(output).to eq("ex=StandardError msg='error time!' user=bob" + Lines::NL)
25
- end
24
+ it "logs exceptions" do
25
+ Lines.log(StandardError.new("error time!"), user: 'bob')
26
+ expect(output).to eq("ex=StandardError msg='error time!' user=bob" + Lines::NL)
27
+ end
26
28
 
27
- it "logs exception backtraces when available" do
28
- ex = (raise "foo" rescue $!)
29
- #expect(ex).not_to eq(nil)
30
- Lines.log(ex)
31
- expect(output).to match(/ex=RuntimeError msg=foo backtrace=\[[^\]]+\]/)
32
- end
29
+ it "logs exception backtraces when available" do
30
+ ex = (raise "foo" rescue $!)
31
+ #expect(ex).not_to eq(nil)
32
+ Lines.log(ex)
33
+ expect(output).to match(/ex=RuntimeError msg=foo backtrace=\[[^\]]+\]/)
34
+ end
33
35
 
34
- it "works with anything" do
35
- Lines.log("anything")
36
- expect(output).to eq('msg=anything' + Lines::NL)
37
- end
36
+ it "works with anything" do
37
+ Lines.log("anything1", "anything2")
38
+ expect(output).to eq('msg=anything2' + Lines::NL)
39
+ end
38
40
 
39
- it "has global context" do
40
- Lines.global["app"] = :self
41
- Lines.log({})
42
- Lines.global.replace({})
43
- expect(output).to eq('app=self' + Lines::NL)
41
+ it "doesn't convert nil args to msg" do
42
+ Lines.log("anything", nil)
43
+ expect(output).to eq('msg=anything' + Lines::NL)
44
+ end
44
45
  end
45
46
 
46
- it "has contextes" do
47
- Lines.context(foo: "bar").log(a: 'b')
48
- expect(output).to eq('a=b foo=bar' + Lines::NL)
47
+ context ".context" do
48
+ it "has contextes" do
49
+ Lines.context(foo: "bar").log(a: 'b')
50
+ expect(output).to eq('a=b foo=bar' + Lines::NL)
51
+ end
52
+
53
+ it "has contextes with blocks" do
54
+ Lines.context(foo: "bar") do |ctx|
55
+ ctx.log(a: 'b')
56
+ end
57
+ expect(output).to eq('a=b foo=bar' + Lines::NL)
58
+ end
59
+
60
+ it "mixes everything" do
61
+ Lines.global[:app] = :self
62
+ ctx = Lines.context(foo: "bar")
63
+ ctx.log('msg', ahoi: true)
64
+ expect(output).to eq('app=self msg=msg ahoi=#t foo=bar' + Lines::NL)
65
+ end
49
66
  end
50
67
 
51
- it "has contextes with blocks" do
52
- Lines.context(foo: "bar") do |ctx|
53
- ctx.log(a: 'b')
68
+ context ".logger" do
69
+ it "is provided for backward-compatibility" do
70
+ l = Lines.logger
71
+ l.info("hi")
72
+ expect(output).to eq('pri=info msg=hi' + Lines::NL)
54
73
  end
55
- expect(output).to eq('a=b foo=bar' + Lines::NL)
56
74
  end
57
75
 
58
- it "has a backward-compatible logger" do
59
- l = Lines.logger
60
- l.info("hi")
61
- expect(output).to eq('pri=info msg=hi' + Lines::NL)
76
+ context ".global" do
77
+ it "prepends data to the line" do
78
+ Lines.global["app"] = :self
79
+ Lines.log 'hey'
80
+ expect(output).to eq('app=self msg=hey' + Lines::NL)
81
+ end
82
+
83
+ it "resolves procs dynamically" do
84
+ count = 0
85
+ Lines.global[:count] = proc{ count += 1 }
86
+ Lines.log 'test1'
87
+ Lines.log 'test2'
88
+ expect(output).to eq(
89
+ 'count=1 msg=test1' + Lines::NL +
90
+ 'count=2 msg=test2' + Lines::NL
91
+ )
92
+ end
62
93
  end
63
94
  end
64
95
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Pfenniger
@@ -45,10 +45,10 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - .gitignore
48
49
  - .rspec
49
50
  - .travis.yml
50
51
  - Gemfile
51
- - Gemfile.lock
52
52
  - LICENSE.txt
53
53
  - README.md
54
54
  - Rakefile
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- lines (0.1.18)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- blankslate (2.1.2.4)
10
- diff-lcs (1.2.1)
11
- json (1.7.7)
12
- parslet (1.5.0)
13
- blankslate (~> 2.0)
14
- rake (0.9.2.2)
15
- rdoc (4.0.0)
16
- json (~> 1.4)
17
- rspec (2.13.0)
18
- rspec-core (~> 2.13.0)
19
- rspec-expectations (~> 2.13.0)
20
- rspec-mocks (~> 2.13.0)
21
- rspec-core (2.13.0)
22
- rspec-expectations (2.13.0)
23
- diff-lcs (>= 1.1.3, < 2.0)
24
- rspec-mocks (2.13.0)
25
-
26
- PLATFORMS
27
- ruby
28
-
29
- DEPENDENCIES
30
- bundler (~> 1.3)
31
- lines!
32
- parslet
33
- rake
34
- rdoc
35
- rspec