multiple_devices_logger 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6446bb60c3aa2bd4d77ede3192dbcea1e7ccf1b
4
- data.tar.gz: fdaabdb5898c18815d5c63bd2ae5af243a0bf30e
3
+ metadata.gz: 8ae86567df1e4256c8ed249fb12684753e072f26
4
+ data.tar.gz: cf237ccec6259568639c300bcd70ecb883fca1f0
5
5
  SHA512:
6
- metadata.gz: 9889055f04b023e94470a1b6ba0358a6247bcd909e6b9534914c958e357fb50316ab0dd7119ba139d62b1fe9bf0bbfebc372e624809cf2e03f01dcdcbefa4af5
7
- data.tar.gz: a6ecb9eebccd038791def48cfd5b045ac970adbd4ae9be86aae332e73ca0e7dd2d1780ff867d2870c439e40372c026bc9244d89ec5398520b37ac734fbc2c83d
6
+ metadata.gz: 2ee12be1c9b809603600ef58a6615ee5973a64a1f43ec6b5e9c5092a000cfdd7fd112e2f9e57a1b0ea3e1545884fc29fce0f4c42d5e9ed6c5ffaeec112a77002
7
+ data.tar.gz: a2e849b612fe6430eb6826e6d106bc196810f3a7be700622a743a288970ec50fe7f32c79a7c7d5a0b721e350326d489507013036d3e2f24827d0b6bcf385fca5
data/README.mdown CHANGED
@@ -68,26 +68,17 @@ Just use `#clear_devices` to clear all registered devices:
68
68
  logger.clear_devices
69
69
  ```
70
70
 
71
- ### Custom formatter for each severity
71
+ ### Custom formatter for each device
72
72
 
73
- `MultipleDevicesLogger` allows you to configure a formatter for each
74
- severity via `#set_formatter` method, here is some examples:
73
+ You can configure a formatter for each device via `:formatter` option, here
74
+ is an example:
75
75
 
76
76
  ```ruby
77
- # Specify formatter as a block
78
- logger.set_formatter(:debug) { |severity, time, progname, message| "[#{severity}] #{message}" }
79
-
80
- # Formatter can also be specified as first argument
81
- my_formatter = -> { |severity, time, progname, message| "#{progname} -> #{message} @ #{time}" }
82
- logger.set_formatter(my_formatter, Logger::ERROR)
83
-
84
- # Operators can also be used
85
- logger.set_formatter('>= warn') { |severity, time, progname, message| "[#{severity}] #{message}" }
86
-
87
- # Like #add_device, many severities can be specified
88
- logger.set_formatter(:debug, :info) { |severity, time, progname, message| "[#{severity}] #{message}" }
77
+ logger.add_device(STDERR, :debug, formatter: -> (severity, time, progname, message) { "[#{severity}] #{message}\n" })
89
78
  ```
90
79
 
80
+ Otherwise default formatter is used.
81
+
91
82
  ## Executing test suite
92
83
 
93
84
  This project is fully tested with [Rspec 3](http://github.com/rspec/rspec).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 2.0.0
@@ -18,7 +18,6 @@ class MultipleDevicesLogger < Logger
18
18
  def initialize
19
19
  super(nil)
20
20
  clear_devices
21
- clear_formatters
22
21
  end
23
22
 
24
23
  def add(severity, message = nil, progname = nil)
@@ -33,9 +32,9 @@ class MultipleDevicesLogger < Logger
33
32
  progname = self.progname
34
33
  end
35
34
  end
36
- text = formatter_for(severity).call(format_severity(severity), Time.now, progname, message)
37
35
  devices_for(severity).each do |device|
38
- device.write(text)
36
+ formatter = device.formatter || self.formatter || @default_formatter
37
+ device.write(formatter.call(format_severity(severity), Time.now, progname, message))
39
38
  end
40
39
  true
41
40
  end
@@ -44,7 +43,13 @@ class MultipleDevicesLogger < Logger
44
43
  def add_device(device, *severities)
45
44
  severities = [severities].flatten
46
45
  options = severities.extract_options!
47
- device = LogDevice.new(device, options) unless device.is_a?(LogDevice)
46
+ formatter = nil
47
+ if options.key?(:formatter)
48
+ formatter = options.delete(:formatter)
49
+ raise ArgumentError.new("Formatter must respond to #call, #{formatter.inspect} given") unless formatter.respond_to?(:call)
50
+ end
51
+ device = LogDevice.new(device, options) unless device.is_a?(Logger::LogDevice)
52
+ device.formatter = formatter
48
53
  if severities.empty?
49
54
  keys = SEVERITIES.values
50
55
  else
@@ -61,10 +66,6 @@ class MultipleDevicesLogger < Logger
61
66
  @devices = {}
62
67
  end
63
68
 
64
- def clear_formatters
65
- @formatters = {}
66
- end
67
-
68
69
  def default_device=(value)
69
70
  @default_device = value.is_a?(LogDevice) ? value : LogDevice.new(value)
70
71
  end
@@ -73,29 +74,10 @@ class MultipleDevicesLogger < Logger
73
74
  @devices[parse_severity(severity)] || [default_device].compact || []
74
75
  end
75
76
 
76
- def formatter_for(severity)
77
- @formatters[parse_severity(severity)] || formatter || @default_formatter
78
- end
79
-
80
77
  def reopen(log = nil)
81
78
  raise NotImplementedError.new("#{self.class}#reopen")
82
79
  end
83
80
 
84
- def set_formatter(*severities)
85
- formatter = block_given? ? Proc.new { |*args| yield(*args) } : severities.shift
86
- raise ArgumentError.new("Formatter must be specified via first parameter or a block, #{formatter.inspect} given") unless formatter.respond_to?(:call)
87
- severities = [severities].flatten
88
- if severities.empty?
89
- keys = SEVERITIES.values
90
- else
91
- keys = severities.map { |severity| parse_severities_with_operator(severity) }.flatten.uniq
92
- end
93
- keys.each do |key|
94
- @formatters[key] = formatter
95
- end
96
- self
97
- end
98
-
99
81
  private
100
82
 
101
83
  def format_message(severity, datetime, progname, msg)
@@ -119,3 +101,9 @@ class MultipleDevicesLogger < Logger
119
101
  end
120
102
 
121
103
  end
104
+
105
+ class Logger::LogDevice
106
+
107
+ attr_accessor :formatter
108
+
109
+ end
@@ -73,10 +73,10 @@ describe MultipleDevicesLogger do
73
73
  logger.add(Logger::WARN, nil, 'BAM!')
74
74
  end
75
75
 
76
- it 'use formatter set via #set_formatter method' do
77
- logger.set_formatter('> debug') { |severity, time, progname, message| "Hello #{message}" }
76
+ it 'use formatter set' do
77
+ logger.add_device(STDERR, '> debug', formatter: -> (severity, time, progname, message) { "Hello #{message}" })
78
78
  expect(STDERR).to receive(:write).with('Hello John')
79
- logger.add(Logger::WARN, 'John')
79
+ logger.add(Logger::INFO, 'John')
80
80
  end
81
81
 
82
82
  it 'use default formatter if not set' do
@@ -86,14 +86,16 @@ describe MultipleDevicesLogger do
86
86
  end
87
87
 
88
88
  it 'use default formatter if formatter set via #set_formatter does not match severity' do
89
- logger.add_device(STDERR, 'debug')
90
- logger.set_formatter(:debug) { |severity, time, progname, message| "Hello #{message}" }
89
+ logger.add_device(STDERR, '<= info', formatter: -> (severity, time, progname, message) { "Hello #{message}" })
91
90
 
92
91
  expect(STDERR).to receive(:write).with(/WARN -- : John/)
93
92
  logger.add(Logger::WARN, 'John')
94
93
 
95
94
  expect(STDERR).to receive(:write).with('Hello John')
96
95
  logger.add(Logger::DEBUG, 'John')
96
+
97
+ expect(STDERR).to receive(:write).with('Hello John')
98
+ logger.add(Logger::INFO, 'John')
97
99
  end
98
100
 
99
101
  end
@@ -304,16 +306,29 @@ describe MultipleDevicesLogger do
304
306
  end
305
307
 
306
308
  it 'given options are forwared to LogDevice constructor' do
307
- expect(Logger::LogDevice).to receive(:new).with(STDOUT, foo: 'bar')
309
+ device = Logger::LogDevice.new(STDOUT)
310
+ expect(Logger::LogDevice).to receive(:new).with(STDOUT, foo: 'bar').and_return(device)
308
311
  logger.add_device(STDOUT, Logger::INFO, foo: 'bar')
309
312
  end
310
313
 
314
+ it 'does not forward :formatter option to log device constructor' do
315
+ device = Logger::LogDevice.new(STDOUT)
316
+ expect(Logger::LogDevice).to receive(:new).with(STDOUT, foo: 'bar').and_return(device)
317
+ logger.add_device(STDOUT, Logger::INFO, foo: 'bar', formatter: -> {})
318
+ end
319
+
311
320
  it 'raise an error for default severity' do
312
321
  expect {
313
322
  logger.add_device(STDERR, 'default')
314
323
  }.to raise_error(ArgumentError, 'Invalid log severity: "default"')
315
324
  end
316
325
 
326
+ it 'raise an error if formatter is not a proc' do
327
+ expect {
328
+ logger.add_device(STDOUT, formatter: :foo)
329
+ }.to raise_error(ArgumentError, 'Formatter must respond to #call, :foo given')
330
+ end
331
+
317
332
  end
318
333
 
319
334
  describe '#clear_devices' do
@@ -327,19 +342,6 @@ describe MultipleDevicesLogger do
327
342
 
328
343
  end
329
344
 
330
- describe '#clear_formatters' do
331
-
332
- it 'removes registerd formatters' do
333
- default_formatter = logger.formatter_for(:info)
334
- formatter = -> { 'hello' }
335
- logger.set_formatter(formatter, :info)
336
- expect {
337
- logger.clear_formatters
338
- }.to change { logger.formatter_for(:info) }.from(formatter).to(default_formatter)
339
- end
340
-
341
- end
342
-
343
345
  describe '#default_device' do
344
346
 
345
347
  it 'is nil by default' do
@@ -401,40 +403,6 @@ describe MultipleDevicesLogger do
401
403
 
402
404
  end
403
405
 
404
- describe '#formatter_for' do
405
-
406
- it 'returns a default formatter by default' do
407
- expect(logger.formatter_for(:debug)).to be_a(Logger::Formatter)
408
- end
409
-
410
- it 'returns formatter set via #formatter= instead' do
411
- formatter = -> {}
412
- logger.formatter = formatter
413
- expect(logger.formatter_for(:info)).to be(formatter)
414
- end
415
-
416
- it 'returns formatter set via #set_formatter method' do
417
- formatter = -> {}
418
- logger.set_formatter(formatter, :debug)
419
- expect(logger.formatter_for(:debug)).to be(formatter)
420
- end
421
-
422
- it 'priority is #set_formatter -> #formatter' do
423
- logger.formatter = -> { 'default' }
424
- logger.set_formatter(:info) { 'info formatter' }
425
- expect(logger.formatter_for(:debug).call).to eq('default')
426
- expect(logger.formatter_for(:info).call).to eq('info formatter')
427
- expect(logger.formatter_for(:warn).call).to eq('default')
428
- end
429
-
430
- it 'raise an error if severity is invalid' do
431
- expect {
432
- logger.formatter_for(:bim)
433
- }.to raise_error(ArgumentError, "Invalid log severity: :bim")
434
- end
435
-
436
- end
437
-
438
406
  describe '#initialize' do
439
407
 
440
408
  it 'accepts no argument' do
@@ -483,73 +451,6 @@ describe MultipleDevicesLogger do
483
451
 
484
452
  end
485
453
 
486
- describe '#set_formatter' do
487
-
488
- it 'sets formatter for devices of given levels' do
489
- logger.add_device(STDERR, '> warn')
490
- logger.add_device(STDOUT, 'debug')
491
- default_formatter = -> { 'world' }
492
- formatter = -> { 'hello' }
493
- logger.formatter = default_formatter
494
-
495
- expect {
496
- logger.set_formatter(formatter, '> info')
497
- }.to change { logger.formatter_for('error') }.from(default_formatter).to(formatter)
498
-
499
- expect(logger.formatter_for('debug')).to be(default_formatter)
500
- end
501
-
502
- it 'use given block if formatter is not specified' do
503
- expect {
504
- logger.set_formatter('> debug') { |severity, time, progname, message| "Hello #{message}" }
505
- }.to change { logger.formatter_for('info').call('WARN', Time.now, nil, 'John') }.to('Hello John')
506
- end
507
-
508
- it 'raise an error if no formatter or block is given' do
509
- expect {
510
- logger.set_formatter('> debug')
511
- }.to raise_error(ArgumentError, 'Formatter must be specified via first parameter or a block, "> debug" given')
512
- end
513
-
514
- it 'can be set for all devices' do
515
- formatter = -> { 'hello' }
516
- logger.add_device(STDERR)
517
- logger.set_formatter(formatter, '> debug')
518
- expect(logger.formatter_for('info')).to be(formatter)
519
- expect(logger.formatter_for('error')).to be(formatter)
520
- end
521
-
522
- it 'raise an error if not a proc' do
523
- expect {
524
- logger.set_formatter(:foo, '> debug')
525
- }.to raise_error(ArgumentError, 'Formatter must be specified via first parameter or a block, :foo given')
526
- end
527
-
528
- it 'raise an error if no parameter is given' do
529
- expect {
530
- logger.set_formatter
531
- }.to raise_error(ArgumentError, 'Formatter must be specified via first parameter or a block, nil given')
532
- end
533
-
534
- it 'severities can be specified as array' do
535
- formatter = -> { 'hello' }
536
- logger.add_device(STDERR)
537
- logger.set_formatter(formatter, ['debug', 'info'])
538
- expect(logger.formatter_for('info')).to be(formatter)
539
- end
540
-
541
- it 'raise an error if severity is invalid' do
542
- expect {
543
- logger.set_formatter(:foo) { 'hello' }
544
- }.to raise_error(ArgumentError, 'Invalid log severity: :foo')
545
- end
546
-
547
- it 'returns self' do
548
- expect(logger.set_formatter(-> {}, :debug)).to be(logger)
549
- end
550
-
551
- end
552
-
553
454
  describe '#warn' do
554
455
 
555
456
  it 'does not output anything if there is no device' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiple_devices_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte