ougai 1.8.5 → 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
  SHA256:
3
- metadata.gz: d04627a9810f76be4d299992b3a2eca66eb66d7d168083962ee3b4a554546d50
4
- data.tar.gz: '089ed84f7e5c8aabd201a82a914dfb0d7d8182b88a3646bb2143060d212544b9'
3
+ metadata.gz: e289dc71320f34d59d1ed29ec87fe87778ac22eec628d627116264c8abdcd403
4
+ data.tar.gz: 3f0d074e2ad4ed429a1c2988034811e94b2ef116a1f13611f7d5949a21af56d9
5
5
  SHA512:
6
- metadata.gz: cfab00111705a26b41ccdbb2888c556902a9b1acda511d191b57ed675a2a835b13b13b79cfff4fe439fefd446f2fa00da93884a36ced0cba8a2e52c3d4e1169d
7
- data.tar.gz: 07edc5901a52a5764b98cb8fdbf504913d0b63a32b6d8d631b0cc73cd079f8ff1e492f0fd98e4d177e9e4fae8656bf46d0476abc167515240176bef8e7df2023
6
+ metadata.gz: 101691c8885df5575d64e603f2e39922a0c0078f50a0685a470de561e5d2db3b01301b8b88eb0e54b23fea86006e02baebd98d2b7c701e9c5876b8df1417c3a7
7
+ data.tar.gz: 3e807cafc0787b005312c527abbddd8bc4ae3834242d1adb3b1fa9b2deb0aebebd363ca1e46c7b120b31794b578c3d68c2bb292a31ce08be0a47edcc192ab5f4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ougai (1.8.5)
4
+ ougai (2.0.0)
5
5
  oj (~> 3.10)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,7 +2,7 @@ Ougai
2
2
  =====
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/ougai.svg)](https://badge.fury.io/rb/ougai)
5
- [![document](https://img.shields.io/badge/document-1.8.3-green.svg)](http://www.rubydoc.info/gems/ougai/)
5
+ [![document](https://img.shields.io/badge/document-1.9.1-green.svg)](http://www.rubydoc.info/gems/ougai/)
6
6
  [![Build Status](https://travis-ci.org/tilfin/ougai.svg?branch=master)](https://travis-ci.org/tilfin/ougai)
7
7
  [![Code Climate](https://codeclimate.com/github/tilfin/ougai/badges/gpa.svg)](https://codeclimate.com/github/tilfin/ougai)
8
8
  [![Test Coverage](https://codeclimate.com/github/tilfin/ougai/badges/coverage.svg)](https://codeclimate.com/github/tilfin/ougai/coverage)
@@ -194,7 +194,18 @@ logger.info('Hello!', user: { name: 'Jiro' }, version: '2.3')
194
194
  ```
195
195
 
196
196
  If any field of with_fields is specified in each log, the field is overridden.
197
- But if the field's type is *Array*, both with_field value and logging value are merged with `concat` and `uniq`.
197
+ If the field's type is *Array*, both with_field value and logging value are merged with `concat` and `uniq`.
198
+
199
+ If the field's type is *Hash*, then values are merged recursively.
200
+
201
+ ```ruby
202
+ logger.with_fields = { version: '1.1.0', user: { name: 'Taro' } }
203
+ logger.debug(user: { age: 19 })
204
+ ```
205
+
206
+ ```json
207
+ {"name":"test","hostname":"mint","pid":30182,"level":20,"time":"2017-07-22T20:52:12.332+09:00","v":0,"version":"1.1.0","msg":"No message","user":{"name":"Taro","age":19}}
208
+ ```
198
209
 
199
210
  ### Create a child logger
200
211
 
@@ -242,6 +253,8 @@ child_logger.debug('This is not outputted')
242
253
 
243
254
  If any field exists in both parent log and child log, the parent value is overridden or merged by child value.
244
255
 
256
+ If the field's type is *Hash*, then values are merged recursively.
257
+
245
258
  ### Hook before logging
246
259
 
247
260
  Setting `before_log` of logger or child an *lambda* with `data` field, a process can be run before log each output.
data/lib/ougai/logger.rb CHANGED
@@ -7,6 +7,7 @@ module Ougai
7
7
  # @attr [Hash] with_fields The fields appending to all logs.
8
8
  # @attr [Proc] before_log Hook before logging.
9
9
  class Logger < ::Logger
10
+ alias_method :super_add, :add
10
11
  include Logging
11
12
 
12
13
  attr_accessor :default_message, :exc_key
@@ -38,9 +39,9 @@ module Ougai
38
39
  # @param logger [Logger] The logger receiving broadcast logs.
39
40
  def self.broadcast(logger)
40
41
  Module.new do |mdl|
41
- define_method(:log) do |*args|
42
- logger.log(*args)
43
- super(*args)
42
+ define_method(:_log) do |*args, &block|
43
+ logger._log(*args, &block)
44
+ super(*args, &block)
44
45
  end
45
46
 
46
47
  define_method(:level=) do |level|
@@ -111,7 +112,7 @@ module Ougai
111
112
  hooks.each do |hook|
112
113
  return false if hook.call(data) == false
113
114
  end
114
- add(severity, data)
115
+ super_add(severity, data)
115
116
  end
116
117
 
117
118
  def to_item(args)
data/lib/ougai/logging.rb CHANGED
@@ -28,7 +28,7 @@ module Ougai
28
28
  # @return [Boolean] true
29
29
  # @see Logging#debug
30
30
  def trace(message = nil, ex = nil, data = nil, &block)
31
- log(TRACE, message, ex, data, block)
31
+ _log(TRACE, message, ex, data, &block)
32
32
  end
33
33
 
34
34
  # Log any one or more of a message, an exception and structured data as DEBUG.
@@ -39,43 +39,42 @@ module Ougai
39
39
  # @yieldreturn [String|Exception|Object|Array] Any one or more of former parameters
40
40
  # @return [Boolean] true
41
41
  def debug(message = nil, ex = nil, data = nil, &block)
42
- log(DEBUG, message, ex, data, block)
42
+ _log(DEBUG, message, ex, data, &block)
43
43
  end
44
44
 
45
45
  # Log any one or more of a message, an exception and structured data as INFO.
46
46
  # @return [Boolean] true
47
47
  # @see Logging#debug
48
48
  def info(message = nil, ex = nil, data = nil, &block)
49
- log(INFO, message, ex, data, block)
49
+ _log(INFO, message, ex, data, &block)
50
50
  end
51
51
 
52
52
  # Log any one or more of a message, an exception and structured data as WARN.
53
53
  # @return [Boolean] true
54
54
  # @see Logging#debug
55
55
  def warn(message = nil, ex = nil, data = nil, &block)
56
- log(WARN, message, ex, data, block)
56
+ _log(WARN, message, ex, data, &block)
57
57
  end
58
58
 
59
59
  # Log any one or more of a message, an exception and structured data as ERROR.
60
60
  # @return [Boolean] true
61
61
  # @see Logging#debug
62
62
  def error(message = nil, ex = nil, data = nil, &block)
63
- log(ERROR, message, ex, data, block)
63
+ _log(ERROR, message, ex, data, &block)
64
64
  end
65
65
 
66
66
  # Log any one or more of a message, an exception and structured data as FATAL.
67
67
  # @return [Boolean] true
68
68
  # @see Logging#debug
69
69
  def fatal(message = nil, ex = nil, data = nil, &block)
70
- log(FATAL, message, ex, data, block)
70
+ _log(FATAL, message, ex, data, &block)
71
71
  end
72
72
 
73
73
  # Log any one or more of a message, an exception and structured data as UNKNOWN.
74
74
  # @return [Boolean] true
75
75
  # @see Logging#debug
76
76
  def unknown(message = nil, ex = nil, data = nil, &block)
77
- args = block ? yield : [message, ex, data]
78
- append(UNKNOWN, args)
77
+ _log(UNKNOWN, message, ex, data, &block)
79
78
  end
80
79
 
81
80
  # Whether the current severity level allows for logging TRACE.
@@ -84,6 +83,26 @@ module Ougai
84
83
  level <= TRACE
85
84
  end
86
85
 
86
+ # Log any one or more of a message, an exception and structured data as specified log level.
87
+ # If the block is given for delay evaluation, it returns them as an array or the one of them as a value.
88
+ # @param severity [Integer] The log level.
89
+ # @param message [String] The message to log. Use default_message if not specified.
90
+ # @param ex [Exception] The exception or the error
91
+ # @param data [Object] Any structured data
92
+ # @yieldreturn [String|Exception|Object|Array] Any one or more of former parameters
93
+ # @return [Boolean] true
94
+ def add(severity, message = nil, ex = nil, data = nil, &block)
95
+ _log(severity, message, ex, data, &block)
96
+ end
97
+
98
+ alias log add
99
+
100
+ def _log(severity, *args)
101
+ severity ||= UNKNOWN
102
+ return true if level > severity
103
+ append(severity, block_given? ? yield : args)
104
+ end
105
+
87
106
  # @private
88
107
  def chain(_severity, _args, _fields, _hooks)
89
108
  raise NotImplementedError
@@ -101,17 +120,12 @@ module Ougai
101
120
  base_data.merge!(inferior_data) do |_, base_v, inferior_v|
102
121
  if base_v.is_a?(Array) and inferior_v.is_a?(Array)
103
122
  (inferior_v + base_v).uniq
123
+ elsif base_v.is_a?(Hash) and inferior_v.is_a?(Hash)
124
+ weak_merge!(base_v, inferior_v)
104
125
  else
105
126
  base_v
106
127
  end
107
128
  end
108
129
  end
109
-
110
- # @private
111
- def log(severity, message, ex, data, block)
112
- return true if level > severity
113
- args = block ? block.call : [message, ex, data]
114
- append(severity, args)
115
- end
116
130
  end
117
131
  end
data/lib/ougai/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ougai
4
- VERSION = '1.8.5'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -390,7 +390,7 @@ describe Ougai::ChildLogger do
390
390
  parent_logger.with_fields = { foo: 11 }
391
391
  logger.with_fields = { bar: '11' }
392
392
  end
393
-
393
+
394
394
  it 'outputs with child fields' do
395
395
  logger.info(log_msg)
396
396
  parent_logger.info(parent_log_msg)
@@ -426,7 +426,7 @@ describe Ougai::ChildLogger do
426
426
  before do
427
427
  parent_logger.with_fields = { foo: 22 }
428
428
  end
429
-
429
+
430
430
  it 'output with new parent fields' do
431
431
  logger.info(log_msg)
432
432
  parent_logger.info(parent_log_msg)
@@ -462,7 +462,7 @@ describe Ougai::ChildLogger do
462
462
  before do
463
463
  logger.with_fields = { bar: '33' }
464
464
  end
465
-
465
+
466
466
  it 'output valid' do
467
467
  logger.info(log_msg)
468
468
  parent_logger.info(parent_log_msg)
@@ -478,35 +478,45 @@ describe Ougai::ChildLogger do
478
478
 
479
479
  context 'grandchild logger' do
480
480
  before do
481
- parent_logger.with_fields = { tag: 'parent', tags: ['parent'] }
481
+ parent_logger.with_fields = { tag: 'parent', tags: ['parent'], event: { module: 'core' } }
482
482
  end
483
483
 
484
- let(:logger) { parent_logger.child(tag: 'child', tags: ['child']) }
485
- let(:grand_logger) { logger.child(tag: 'grandchild', tags: ['grandchild']) }
484
+ let(:logger) { parent_logger.child(tag: 'child', tags: ['child'], event: { dataset: 'core.child' }) }
485
+ let(:grand_logger) { logger.child(tag: 'grandchild', tags: ['grandchild'], event: { action: 'log-action' }) }
486
486
 
487
487
  it 'outputs with all merged fields' do
488
488
  grand_logger.info('Hi', foo: 3)
489
489
  logger.info(log_msg, foo: 2)
490
- parent_logger.info(parent_log_msg, foo: 10)
491
- parent_logger.info('Good evening!', foo: 11)
490
+ parent_logger.info(parent_log_msg, foo: 10, event: { module: 'service' })
491
+ parent_logger.info('Good evening!', foo: 11, event: { duration: 150 })
492
492
 
493
493
  expect(items[0]).to be_log_message('Hi', log_level)
494
- expect(items[0]).to include(tag: 'grandchild', tags: ['parent', 'child', 'grandchild'], foo: 3)
494
+ expect(items[0]).to include(
495
+ tag: 'grandchild',
496
+ tags: ['parent', 'child', 'grandchild'],
497
+ foo: 3,
498
+ event: { module: 'core', dataset: 'core.child', action: 'log-action' }
499
+ )
495
500
 
496
501
  expect(items[1]).to be_log_message(log_msg, log_level)
497
- expect(items[1]).to include(tag: 'child', tags: ['parent', 'child'], foo: 2)
502
+ expect(items[1]).to include(
503
+ tag: 'child',
504
+ tags: ['parent', 'child'],
505
+ foo: 2,
506
+ event: { module: 'core', dataset: 'core.child' }
507
+ )
498
508
 
499
509
  expect(items[2]).to be_log_message(parent_log_msg, log_level)
500
- expect(items[2]).to include(tag: 'parent', tags: ['parent'], foo: 10)
510
+ expect(items[2]).to include(tag: 'parent', tags: ['parent'], foo: 10, event: { module: 'service' })
501
511
  expect(items[3]).to be_log_message('Good evening!', log_level)
502
- expect(items[3]).to include(tag: 'parent', tags: ['parent'], foo: 11)
512
+ expect(items[3]).to include(tag: 'parent', tags: ['parent'], foo: 11, event: { module: 'core', duration: 150 })
503
513
  end
504
514
 
505
515
  context 'after updating child logger with_fields' do
506
516
  before do
507
517
  logger.with_fields = { bar: '33' }
508
518
  end
509
-
519
+
510
520
  it 'outputs with child fields' do
511
521
  logger.info(log_msg)
512
522
  expect(items[0]).to be_log_message(log_msg, log_level)
data/spec/logger_spec.rb CHANGED
@@ -349,6 +349,14 @@ describe Ougai::Logger do
349
349
  it_behaves_like 'log'
350
350
  end
351
351
 
352
+ describe '#unknown' do
353
+ let(:log_level) { 70 }
354
+ let(:log_msg) { 'unknown message' }
355
+ let(:method) { 'unknown' }
356
+
357
+ it_behaves_like 'log'
358
+ end
359
+
352
360
  describe '#level' do
353
361
  context 'DEBUG' do
354
362
  let(:log_msg) { 'log message' }
data/spec/logging_spec.rb CHANGED
@@ -3,7 +3,14 @@ require 'spec_helper'
3
3
  describe Ougai::Logging do
4
4
  subject do
5
5
  m = described_class
6
- Class.new { include m }.new
6
+
7
+ Class.new do
8
+ include m
9
+
10
+ def level
11
+ -1
12
+ end
13
+ end.new
7
14
  end
8
15
 
9
16
  describe '#weak_merge!' do
@@ -17,6 +24,15 @@ describe Ougai::Logging do
17
24
  expect(result[:bar]).to eq('base')
18
25
  expect(result[:baz]).to eq(['B', 'A'])
19
26
  end
27
+
28
+ it 'merges hashes recursively' do
29
+ result = nil
30
+ subject.instance_eval do
31
+ result = weak_merge!({ foo: { bar: { baz: 15 } } },
32
+ { foo: { bar: { extra: 10 }, nested: 'string' } })
33
+ end
34
+ expect(result).to eq({ foo: { bar: { baz: 15, extra: 10 }, nested: 'string' } })
35
+ end
20
36
  end
21
37
 
22
38
  describe '#chain' do
@@ -30,4 +46,62 @@ describe Ougai::Logging do
30
46
  expect{ subject.send(:append, :arg1, :arg2) }.to raise_error(NotImplementedError)
31
47
  end
32
48
  end
49
+
50
+ describe '#add' do
51
+ context 'severity is specified level' do
52
+ it 'calls append with specified level' do
53
+ data = double('data')
54
+ expect(subject).to receive(:append).with(::Logger::Severity::DEBUG, ['debug message', data, nil])
55
+ subject.add(::Logger::Severity::DEBUG, 'debug message', data)
56
+ end
57
+ end
58
+
59
+ context 'severity is nil' do
60
+ it 'calls append with UNKNOWN level' do
61
+ expect(subject).to receive(:append).with(::Logger::Severity::UNKNOWN, ['message', nil, nil])
62
+ subject.add(nil, 'message')
63
+ end
64
+ end
65
+
66
+ context 'with block that yields message' do
67
+ it 'calls append with yielded message' do
68
+ expect(subject).to receive(:append).with(::Logger::Severity::WARN, 'block message')
69
+ subject.add(::Logger::Severity::WARN) { 'block message' }
70
+ end
71
+ end
72
+
73
+ context 'with block that yields array' do
74
+ it 'calls append with yielded array' do
75
+ data = double('data')
76
+ expect(subject).to receive(:append).with(::Logger::Severity::WARN, ['block message', data])
77
+ subject.add(::Logger::Severity::WARN) { ['block message', data] }
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#log' do
83
+ context 'severity is specified' do
84
+ it 'calls append with specified level' do
85
+ ex = Exception.new
86
+ expect(subject).to receive(:append).with(::Logger::Severity::FATAL, ['fatal message', ex, nil])
87
+ subject.log(::Logger::Severity::FATAL, 'fatal message', ex)
88
+ end
89
+ end
90
+
91
+ context 'severity is nil' do
92
+ it 'calls append with UNKNOWN level' do
93
+ expect(subject).to receive(:append).with(::Logger::Severity::UNKNOWN, ['message', nil, nil])
94
+ subject.log(nil, 'message')
95
+ end
96
+ end
97
+
98
+ context 'with block' do
99
+ it 'calls append with yielded arguments' do
100
+ ex = Exception.new
101
+ data = double('data')
102
+ expect(subject).to receive(:append).with(::Logger::Severity::INFO, ['block message', ex, data])
103
+ subject.log(::Logger::Severity::INFO) { ['block message', ex, data] }
104
+ end
105
+ end
106
+ end
33
107
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ougai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.5
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshimitsu Takahashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-04 00:00:00.000000000 Z
11
+ date: 2021-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -114,25 +114,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 2.4.0
117
+ version: 2.5.0
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  requirements:
120
120
  - - ">="
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubygems_version: 3.0.1
124
+ rubygems_version: 3.1.2
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: JSON logger compatible with node-bunyan or pino is capable of handling structured
128
128
  data easily.
129
129
  test_files:
130
- - spec/formatters/pino_spec.rb
131
- - spec/formatters/base_spec.rb
132
- - spec/formatters/bunyan_spec.rb
133
- - spec/formatters/readable_spec.rb
134
130
  - spec/child_logger_spec.rb
135
131
  - spec/logging_spec.rb
136
- - spec/logger_spec.rb
137
132
  - spec/ougai_spec.rb
138
133
  - spec/spec_helper.rb
134
+ - spec/formatters/pino_spec.rb
135
+ - spec/formatters/readable_spec.rb
136
+ - spec/formatters/base_spec.rb
137
+ - spec/formatters/bunyan_spec.rb
138
+ - spec/logger_spec.rb