madvertise-logging 0.9.4 → 1.0.0.rc1
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/.rvmrc +1 -1
- data/Gemfile +1 -1
- data/lib/madvertise/logging/improved_logger.rb +11 -5
- data/lib/madvertise/logging/version.rb +1 -1
- data/spec/improved_logger_spec.rb +124 -153
- metadata +7 -10
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use --create ruby-1.9.3-
|
1
|
+
rvm use --create ruby-1.9.3-p392@madvertise-logging
|
data/Gemfile
CHANGED
@@ -178,10 +178,16 @@ module Madvertise
|
|
178
178
|
|
179
179
|
def add(severity, message, attribs = {})
|
180
180
|
severity = severity.is_a?(Symbol) ? self.class.severities[severity] : severity
|
181
|
-
|
182
|
-
|
183
|
-
|
181
|
+
|
182
|
+
attribs.merge!(called_from) if @log_caller
|
183
|
+
attribs.merge!(token: @token) if @token
|
184
|
+
attribs = attribs.map do |k,v|
|
185
|
+
"#{k}=#{v.to_s.clean_quote}"
|
186
|
+
end.join(' ')
|
187
|
+
|
188
|
+
message = "#{message} #{attribs}" if attribs.length > 0
|
184
189
|
logger.add(severity) { message }
|
190
|
+
|
185
191
|
return nil
|
186
192
|
end
|
187
193
|
|
@@ -233,8 +239,8 @@ module Madvertise
|
|
233
239
|
line.match(/(improved_logger|multi_logger)\.rb/).nil?
|
234
240
|
end
|
235
241
|
|
236
|
-
file,
|
237
|
-
|
242
|
+
file, line, _ = location.split(':')
|
243
|
+
{ :file => file, :line => line }
|
238
244
|
end
|
239
245
|
|
240
246
|
def create_backend
|
@@ -1,106 +1,71 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
require 'tempfile'
|
4
|
-
|
5
|
-
include Madvertise::Logging
|
6
|
-
|
7
|
-
RSpec::Matchers.define :have_received_message do |expected|
|
8
|
-
match do |actual|
|
9
|
-
@last = IO.readlines(actual).last rescue nil
|
10
|
-
@last ? @last.match(Regexp.new(expected)) : false
|
11
|
-
end
|
12
|
-
|
13
|
-
failure_message_for_should do |actual|
|
14
|
-
"expected #{@last.inspect} to contain #{expected}"
|
15
|
-
end
|
16
|
-
|
17
|
-
failure_message_for_should_not do |actual|
|
18
|
-
"expected #{@last.inspect} to not contain #{expected}"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
3
|
describe ImprovedLogger do
|
23
4
|
|
24
|
-
|
25
|
-
Tempfile.new("spec").tap do |tmpfile|
|
26
|
-
@logfile = tmpfile.path
|
27
|
-
tmpfile.close
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
after(:all) do
|
32
|
-
File.unlink(@logfile) rescue nil
|
33
|
-
end
|
34
|
-
|
35
|
-
before(:each) do
|
36
|
-
File.unlink(@logfile) rescue nil
|
37
|
-
@logger = ImprovedLogger.new(@logfile)
|
38
|
-
@logger.level = :debug
|
39
|
-
end
|
5
|
+
let(:logger) { ImprovedLogger.new(:document) }
|
40
6
|
|
41
|
-
|
7
|
+
before(:each) { logger.level = :debug }
|
42
8
|
|
43
|
-
|
44
|
-
its(:logger) { should_not be_nil }
|
9
|
+
subject { logger.messages }
|
45
10
|
|
46
11
|
ImprovedLogger.severities.keys.each do |level|
|
47
12
|
describe level do
|
48
|
-
|
49
|
-
before { @logger.send(level, "test") }
|
13
|
+
before { logger.send(level, "testing #{level}") }
|
50
14
|
let(:prefix) { level == :unknown ? "ANY" : level.to_s.upcase }
|
51
|
-
it
|
15
|
+
it "logs #{level} messages" do
|
16
|
+
subject.last[:message].should == "testing #{level}"
|
17
|
+
end
|
52
18
|
end
|
53
19
|
end
|
54
20
|
|
55
|
-
it "
|
56
|
-
|
57
|
-
|
58
|
-
@logger.write("Info test2")
|
59
|
-
@logfile.should have_received_message(/\[INFO\].*Info test2/)
|
21
|
+
it "logs info level messages with <<" do
|
22
|
+
logger << "Info test <<"
|
23
|
+
subject.last[:message].should == "Info test <<"
|
60
24
|
end
|
61
25
|
|
62
|
-
it "
|
63
|
-
|
64
|
-
|
26
|
+
it "logs info level messages with write" do
|
27
|
+
logger.write("Info test write")
|
28
|
+
subject.last[:message].should == "Info test write"
|
65
29
|
end
|
66
30
|
|
67
|
-
it "
|
68
|
-
|
69
|
-
|
70
|
-
@logger.debug { ["debug message", {key: "value"}] }
|
71
|
-
@logfile.should have_received_message(/debug message.*key=value/)
|
31
|
+
it "supports additional attributes" do
|
32
|
+
logger.info("foo", key: "value", test: "with space")
|
33
|
+
subject.last[:message].should == 'foo key=value test="with space"'
|
72
34
|
end
|
73
35
|
|
74
|
-
it "
|
75
|
-
|
76
|
-
|
77
|
-
@logger.logger.should == l
|
36
|
+
it "supports lazy-evaluation via blocks" do
|
37
|
+
logger.debug { "debug message" }
|
38
|
+
subject.last[:message].should == "debug message"
|
78
39
|
end
|
79
40
|
|
80
|
-
it "
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
41
|
+
it "supports lazy-evaluation with attributes" do
|
42
|
+
logger.debug { ["debug message", {key: "value"}] }
|
43
|
+
subject.last[:message].should == "debug message key=value"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "accepts a different backend" do
|
47
|
+
l = Logger.new('/dev/null')
|
48
|
+
logger.logger = l
|
49
|
+
logger.logger.should == l
|
85
50
|
end
|
86
51
|
|
87
52
|
describe :log_caller do
|
88
|
-
it "
|
89
|
-
f =
|
53
|
+
it "logs the caller file and line number" do
|
54
|
+
f = __FILE__
|
90
55
|
l = __LINE__ + 3
|
91
56
|
|
92
|
-
|
93
|
-
|
94
|
-
|
57
|
+
logger.log_caller = true
|
58
|
+
logger.info("Caller test")
|
59
|
+
subject.last[:message].should == "Caller test file=#{f} line=#{l}"
|
95
60
|
end
|
96
61
|
|
97
|
-
it "
|
62
|
+
it "does not log the caller file and line number" do
|
98
63
|
f = File.basename(__FILE__)
|
99
64
|
l = __LINE__ + 3
|
100
65
|
|
101
|
-
|
102
|
-
|
103
|
-
|
66
|
+
logger.log_caller = false
|
67
|
+
logger.info("Caller test")
|
68
|
+
subject.last[:message].should_not == "Caller test file=#{f} line=#{l}"
|
104
69
|
end
|
105
70
|
end
|
106
71
|
|
@@ -118,44 +83,39 @@ describe ImprovedLogger do
|
|
118
83
|
end
|
119
84
|
end
|
120
85
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
before { @logger.exception(exc) }
|
125
|
-
it { should have_received_message("exception class=RuntimeError reason=\"Test error\"") }
|
86
|
+
it "logs an exception object" do
|
87
|
+
logger.exception(exc)
|
88
|
+
subject.last[:message].should == "exception class=RuntimeError reason=\"Test error\" message= backtrace=\"['/home/jdoe/app/libexec/app.rb:1:in `foo'']\""
|
126
89
|
end
|
127
90
|
|
128
|
-
|
129
|
-
|
130
|
-
|
91
|
+
it "logs an exception object and prefix" do
|
92
|
+
logger.exception(exc, "app failed to foo")
|
93
|
+
subject.last[:message].should == "exception class=RuntimeError reason=\"Test error\" message=\"app failed to foo\" backtrace=\"['/home/jdoe/app/libexec/app.rb:1:in `foo'']\""
|
131
94
|
end
|
132
95
|
end
|
133
96
|
|
134
97
|
describe :clean_trace do
|
135
|
-
subject {
|
98
|
+
subject { logger.clean_trace(fake_trace) }
|
136
99
|
it { should include("/home/jdoe/app/libexec/app.rb:1:in `foo'") }
|
137
100
|
it { should_not include("/usr/lib/ruby/gems/1.8/gems/madvertise-logging-0.1.0/lib/madvertise/logging/improved_logger.rb:42: in `info'") }
|
138
101
|
end
|
139
102
|
|
140
103
|
it "should support silencing" do
|
141
|
-
|
104
|
+
logger.silence do |logger|
|
142
105
|
logger.info "This should never be logged"
|
143
106
|
end
|
144
107
|
|
145
|
-
|
146
|
-
|
147
|
-
@logger.info "This should be logged"
|
148
|
-
@logfile.should have_received_message("This should be logged")
|
108
|
+
subject.last.should be_nil
|
149
109
|
end
|
150
110
|
|
151
111
|
it "should not discard messages if silencer is disabled globally" do
|
152
112
|
ImprovedLogger.silencer = false
|
153
113
|
|
154
|
-
|
114
|
+
logger.silence do |logger|
|
155
115
|
logger.info "This should actually be logged"
|
156
116
|
end
|
157
117
|
|
158
|
-
|
118
|
+
subject.last[:message].should == "This should actually be logged"
|
159
119
|
|
160
120
|
ImprovedLogger.silencer = true
|
161
121
|
end
|
@@ -163,13 +123,13 @@ describe ImprovedLogger do
|
|
163
123
|
it "should support a token" do
|
164
124
|
token = "3d5e27f7-b97c-4adc-b1fd-adf1bd4314e0"
|
165
125
|
|
166
|
-
|
167
|
-
|
168
|
-
|
126
|
+
logger.token = token
|
127
|
+
logger.info "This should include a token"
|
128
|
+
subject.last[:message].should match(token)
|
169
129
|
|
170
|
-
|
171
|
-
|
172
|
-
|
130
|
+
logger.token = nil
|
131
|
+
logger.info "This should not include a token"
|
132
|
+
subject.last[:message].should_not match(token)
|
173
133
|
end
|
174
134
|
|
175
135
|
it "should support save/restore on tokens" do
|
@@ -178,34 +138,34 @@ describe ImprovedLogger do
|
|
178
138
|
|
179
139
|
obj = Object.new
|
180
140
|
|
181
|
-
|
182
|
-
|
183
|
-
|
141
|
+
logger.token = token1
|
142
|
+
logger.info "This should include token1"
|
143
|
+
subject.last[:message].should match(token1)
|
184
144
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
145
|
+
logger.save_token(obj)
|
146
|
+
logger.token = token2
|
147
|
+
logger.info "This should include token2"
|
148
|
+
subject.last[:message].should match(token2)
|
189
149
|
|
190
|
-
|
191
|
-
|
192
|
-
|
150
|
+
logger.restore_token(obj)
|
151
|
+
logger.info "This should include token1"
|
152
|
+
subject.last[:message].should match(token1)
|
193
153
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
154
|
+
logger.token = nil
|
155
|
+
logger.info "This should not include a token"
|
156
|
+
subject.last[:message].should_not match(token1)
|
157
|
+
subject.last[:message].should_not match(token2)
|
198
158
|
end
|
199
159
|
|
200
160
|
it "should fall back to stderr if logfile is not writable" do
|
201
161
|
$stderr.should_receive(:write).with(/not writable.*STDERR/)
|
202
162
|
|
203
163
|
@logfile = "/not/writable/spec.log"
|
204
|
-
|
205
|
-
|
164
|
+
logger = ImprovedLogger.new(@logfile)
|
165
|
+
logger.level = :debug
|
206
166
|
|
207
167
|
$stderr.should_receive(:write).with(/test/)
|
208
|
-
|
168
|
+
logger.info "test"
|
209
169
|
end
|
210
170
|
|
211
171
|
it "should fallback to standard logger if syslogger gem is missing" do
|
@@ -213,50 +173,33 @@ describe ImprovedLogger do
|
|
213
173
|
$:.replace($: - syslogger_paths)
|
214
174
|
|
215
175
|
$stderr.should_receive(:write).with(/reverting to STDERR/)
|
216
|
-
|
217
|
-
|
176
|
+
logger = ImprovedLogger.new(:syslog)
|
177
|
+
logger.logger.should be_instance_of(Logger)
|
218
178
|
|
219
179
|
$:.replace($: + syslogger_paths)
|
220
180
|
end
|
221
181
|
|
222
182
|
context "should behave like write-only IO and" do
|
223
|
-
|
224
|
-
@logger.should_receive(:close)
|
225
|
-
@logger.close_write
|
226
|
-
end
|
183
|
+
subject { logger }
|
227
184
|
|
228
|
-
|
229
|
-
its(:
|
185
|
+
it { should be_a IO }
|
186
|
+
its(:logger) { should_not be_nil }
|
187
|
+
its(:flush) { should == logger }
|
188
|
+
its(:set_encoding) { should == logger }
|
230
189
|
its(:sync) { should == true }
|
231
190
|
its(:tty?) { should == false }
|
232
191
|
|
233
|
-
it "should
|
234
|
-
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
it "should support print" do
|
239
|
-
$,, old = ' ', $,
|
240
|
-
@logger.print("foo", "bar", 123, ["baz", 345])
|
241
|
-
@logfile.should have_received_message("foo bar 123 baz 345")
|
242
|
-
$, = old
|
243
|
-
end
|
244
|
-
|
245
|
-
it "should support puts" do
|
246
|
-
@logger.puts("a", "b")
|
247
|
-
@logfile.should have_received_message("b")
|
248
|
-
@logger.puts(["c", "d"])
|
249
|
-
@logfile.should have_received_message("d")
|
250
|
-
@logger.puts(1, 2, 3)
|
251
|
-
@logfile.should have_received_message("3")
|
192
|
+
it "should close on close_write" do
|
193
|
+
logger.should_receive(:close)
|
194
|
+
logger.close_write
|
252
195
|
end
|
253
196
|
|
254
197
|
it "should not implement closed?" do
|
255
|
-
expect {
|
198
|
+
expect { logger.closed? }.to raise_error(NotImplementedError)
|
256
199
|
end
|
257
200
|
|
258
201
|
it "should not implement sync=" do
|
259
|
-
expect {
|
202
|
+
expect { logger.sync = false }.to raise_error(NotImplementedError)
|
260
203
|
end
|
261
204
|
|
262
205
|
it "should implement readbyte, readchar, readline" do
|
@@ -265,8 +208,8 @@ describe ImprovedLogger do
|
|
265
208
|
:readchar => :getc,
|
266
209
|
:readline => :gets,
|
267
210
|
}.each do |m, should|
|
268
|
-
|
269
|
-
expect {
|
211
|
+
logger.should_receive(should)
|
212
|
+
expect { logger.send(m) }.to raise_error(IOError)
|
270
213
|
end
|
271
214
|
end
|
272
215
|
|
@@ -290,23 +233,50 @@ describe ImprovedLogger do
|
|
290
233
|
:ungetc
|
291
234
|
].each do |m|
|
292
235
|
it "should raise IOError for method #{m}" do
|
293
|
-
expect {
|
236
|
+
expect { logger.send(m) }.to raise_error(IOError)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
context "print functions" do
|
241
|
+
subject { logger.messages }
|
242
|
+
|
243
|
+
it "should support printf" do
|
244
|
+
logger.printf("%.2f %s", 1.12345, "foo")
|
245
|
+
subject.last[:message].should == "1.12 foo"
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should support print" do
|
249
|
+
$,, old = ' ', $,
|
250
|
+
logger.print("foo", "bar", 123, ["baz", 345])
|
251
|
+
subject.last[:message].should == "foo bar 123 baz 345"
|
252
|
+
$, = old
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should support puts" do
|
256
|
+
logger.puts("a", "b")
|
257
|
+
subject.last[:message].should == "b"
|
258
|
+
logger.puts(["c", "d"])
|
259
|
+
subject.last[:message].should == "d"
|
260
|
+
logger.puts(1, 2, 3)
|
261
|
+
subject.last[:message].should == "3"
|
294
262
|
end
|
295
263
|
end
|
296
264
|
end
|
297
265
|
|
298
266
|
context "buffer backend" do
|
299
|
-
|
267
|
+
let(:logger) { ImprovedLogger.new(:buffer) }
|
268
|
+
subject { logger }
|
269
|
+
|
300
270
|
its(:sync) { should == false }
|
301
271
|
|
302
272
|
it "should support a buffered logger" do
|
303
|
-
|
304
|
-
|
273
|
+
logger.info "test"
|
274
|
+
logger.buffer.should match(/test/)
|
305
275
|
end
|
306
276
|
end
|
307
277
|
|
308
278
|
context "document backend" do
|
309
|
-
|
279
|
+
let(:logger) { ImprovedLogger.new(:document) }
|
310
280
|
|
311
281
|
before do
|
312
282
|
@msg = "test"
|
@@ -323,21 +293,22 @@ describe ImprovedLogger do
|
|
323
293
|
end
|
324
294
|
|
325
295
|
it "should store all messages as documents" do
|
326
|
-
|
327
|
-
|
296
|
+
logger.info(@msg)
|
297
|
+
logger.messages.first.should == @expected
|
328
298
|
end
|
329
299
|
|
330
300
|
it "should add custom attributes" do
|
331
301
|
attrs = {txid: 1234}
|
332
|
-
|
333
|
-
|
334
|
-
|
302
|
+
logger.logger.attrs = attrs
|
303
|
+
logger.info(@msg)
|
304
|
+
logger.messages.first.should == attrs.merge(@expected)
|
335
305
|
end
|
336
306
|
|
337
307
|
end
|
338
308
|
|
339
309
|
context "syslog backend" do
|
340
|
-
|
310
|
+
let(:logger) { ImprovedLogger.new(:syslog) }
|
311
|
+
subject { logger }
|
341
312
|
its(:sync) { should == true }
|
342
313
|
its(:logger) { should be_instance_of(Syslogger) }
|
343
314
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: madvertise-logging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0.rc1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Benedikt Böhm
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Advanced logging classes with buffer backend, transactions, multi logger,
|
15
15
|
etc
|
@@ -58,19 +58,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
58
|
version: '0'
|
59
59
|
segments:
|
60
60
|
- 0
|
61
|
-
hash:
|
61
|
+
hash: 438765603245301445
|
62
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
|
-
- - ! '
|
65
|
+
- - ! '>'
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
68
|
-
segments:
|
69
|
-
- 0
|
70
|
-
hash: 1370509839467996557
|
67
|
+
version: 1.3.1
|
71
68
|
requirements: []
|
72
69
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.8.
|
70
|
+
rubygems_version: 1.8.25
|
74
71
|
signing_key:
|
75
72
|
specification_version: 3
|
76
73
|
summary: Advanced logging classes with buffer backend, transactions, multi logger,
|