multiple_devices_logger 1.0.0 → 1.1.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: e1ab6f46b22a880122cfcf4c6b84ce37c4fb229b
4
- data.tar.gz: c69bcdc6a13537e8267e98435d80d4fc48f3be97
3
+ metadata.gz: b6446bb60c3aa2bd4d77ede3192dbcea1e7ccf1b
4
+ data.tar.gz: fdaabdb5898c18815d5c63bd2ae5af243a0bf30e
5
5
  SHA512:
6
- metadata.gz: 9938b033da846d237f8ab0746e84092e1024198026a3afd020661403eafc49a5e6ab0449a588078577f9658fcaa0784492296c0eb15d10fe17ef29e647321b72
7
- data.tar.gz: c100f7447b874b85245ff97b3fed4d4c770c5977bfd1f242455d32123904e60052595dd147ffe0c9742e5f918ccf6db17f15892770f66cb09c007ce8002e7193
6
+ metadata.gz: 9889055f04b023e94470a1b6ba0358a6247bcd909e6b9534914c958e357fb50316ab0dd7119ba139d62b1fe9bf0bbfebc372e624809cf2e03f01dcdcbefa4af5
7
+ data.tar.gz: a6ecb9eebccd038791def48cfd5b045ac970adbd4ae9be86aae332e73ca0e7dd2d1780ff867d2870c439e40372c026bc9244d89ec5398520b37ac734fbc2c83d
data/README.mdown CHANGED
@@ -68,6 +68,26 @@ Just use `#clear_devices` to clear all registered devices:
68
68
  logger.clear_devices
69
69
  ```
70
70
 
71
+ ### Custom formatter for each severity
72
+
73
+ `MultipleDevicesLogger` allows you to configure a formatter for each
74
+ severity via `#set_formatter` method, here is some examples:
75
+
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}" }
89
+ ```
90
+
71
91
  ## Executing test suite
72
92
 
73
93
  This project is fully tested with [Rspec 3](http://github.com/rspec/rspec).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -18,6 +18,7 @@ class MultipleDevicesLogger < Logger
18
18
  def initialize
19
19
  super(nil)
20
20
  clear_devices
21
+ clear_formatters
21
22
  end
22
23
 
23
24
  def add(severity, message = nil, progname = nil)
@@ -32,7 +33,7 @@ class MultipleDevicesLogger < Logger
32
33
  progname = self.progname
33
34
  end
34
35
  end
35
- text = format_message(format_severity(severity), Time.now, progname, message)
36
+ text = formatter_for(severity).call(format_severity(severity), Time.now, progname, message)
36
37
  devices_for(severity).each do |device|
37
38
  device.write(text)
38
39
  end
@@ -60,6 +61,10 @@ class MultipleDevicesLogger < Logger
60
61
  @devices = {}
61
62
  end
62
63
 
64
+ def clear_formatters
65
+ @formatters = {}
66
+ end
67
+
63
68
  def default_device=(value)
64
69
  @default_device = value.is_a?(LogDevice) ? value : LogDevice.new(value)
65
70
  end
@@ -68,12 +73,35 @@ class MultipleDevicesLogger < Logger
68
73
  @devices[parse_severity(severity)] || [default_device].compact || []
69
74
  end
70
75
 
76
+ def formatter_for(severity)
77
+ @formatters[parse_severity(severity)] || formatter || @default_formatter
78
+ end
79
+
71
80
  def reopen(log = nil)
72
81
  raise NotImplementedError.new("#{self.class}#reopen")
73
82
  end
74
83
 
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
+
75
99
  private
76
100
 
101
+ def format_message(severity, datetime, progname, msg)
102
+ raise NotImplementedError.new("#{self.class}#format_message")
103
+ end
104
+
77
105
  def parse_severity(value)
78
106
  int_value = value.is_a?(Fixnum) ? value : (Integer(value.to_s) rescue nil)
79
107
  return int_value if SEVERITIES.values.include?(int_value)
@@ -73,12 +73,29 @@ describe MultipleDevicesLogger do
73
73
  logger.add(Logger::WARN, nil, 'BAM!')
74
74
  end
75
75
 
76
- it 'a formatter could be given' do
76
+ it 'use formatter set via #set_formatter method' do
77
+ logger.set_formatter('> debug') { |severity, time, progname, message| "Hello #{message}" }
78
+ expect(STDERR).to receive(:write).with('Hello John')
79
+ logger.add(Logger::WARN, 'John')
80
+ end
81
+
82
+ it 'use default formatter if not set' do
77
83
  logger.formatter = -> (severity, time, progname, message) { "Hello #{progname}: #{message}" }
78
84
  expect(STDERR).to receive(:write).with('Hello World: cool')
79
85
  logger.add(Logger::WARN, 'cool', 'World')
80
86
  end
81
87
 
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}" }
91
+
92
+ expect(STDERR).to receive(:write).with(/WARN -- : John/)
93
+ logger.add(Logger::WARN, 'John')
94
+
95
+ expect(STDERR).to receive(:write).with('Hello John')
96
+ logger.add(Logger::DEBUG, 'John')
97
+ end
98
+
82
99
  end
83
100
 
84
101
  describe '#add_device' do
@@ -310,6 +327,19 @@ describe MultipleDevicesLogger do
310
327
 
311
328
  end
312
329
 
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
+
313
343
  describe '#default_device' do
314
344
 
315
345
  it 'is nil by default' do
@@ -371,6 +401,40 @@ describe MultipleDevicesLogger do
371
401
 
372
402
  end
373
403
 
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
+
374
438
  describe '#initialize' do
375
439
 
376
440
  it 'accepts no argument' do
@@ -419,6 +483,73 @@ describe MultipleDevicesLogger do
419
483
 
420
484
  end
421
485
 
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
+
422
553
  describe '#warn' do
423
554
 
424
555
  it 'does not output anything if there is no device' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiple_devices_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport