ougai 0.9.0 → 1.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: 7662273f5608fda137ffbfb6286a7361cef52dc5
4
- data.tar.gz: f500797dfdcd3b6c75b58c17c7644df8fb786430
3
+ metadata.gz: d30c9475aab949b8119bbb1bb5b921b82cab591f
4
+ data.tar.gz: 1a60805fd8a9602104c4f45180b54f1db48c5a81
5
5
  SHA512:
6
- metadata.gz: 4d7247de5ed99e0a7cfb45bd99e4da264262ea237e0ab0c4e1720396bd8acd973d0f93f2880841f62f0ffcfaf7f9b089bf966b349d59434823b871bfaa4a49f9
7
- data.tar.gz: af91ab240776919d8a17262f7a3cc5dee143041c04fb2cbb303f9515b2b06abdb50bf224928f210e1864ffae6f9ed7f6a8cd541b58e236da849386af1ee4d788
6
+ metadata.gz: abb0ce860369b3ff1bf6f3f853e965dae781fd73cf341c954f2fd5f1d9316027850fe6c29eb339cf3b4d9e4abc870af1f39e27a9eb11845ac3532905ab89ed25
7
+ data.tar.gz: 31bd2e3c9795548c6b47c4a874b0dee68fd98242037040601fc81f8806c2cdff5b3b005cbb708e83ddbe72c168551b67ad437f1dafa855d2f5db63b00942c502
data/README.md CHANGED
@@ -168,6 +168,22 @@ end
168
168
 
169
169
  To specify more than one of a message, an exception and custom data, the block returns them as an array.
170
170
 
171
+ ### Adding custom fields to all logs
172
+
173
+ The fields of `with_fields` add to all logs as is.
174
+
175
+ ```ruby
176
+ logger.with_fields = { version: '1.1.0' }
177
+ logger.debug(user: { name: 'Taro', age: 19 })
178
+ logger.info('Hello!', user: { name: 'Jiro' }, version: '2.3')
179
+ ```
180
+
181
+ ```json
182
+ {"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}}
183
+ {"name":"test","hostname":"mint","pid":30308,"level":30,"time":"2017-07-22T20:53:54.314+09:00","v":0,"version":"2.3","user":{"name":"Jiro"},"msg":"Hello!"}
184
+ ```
185
+
186
+ If any field of with_fields is specified in each log, the field is overridden.
171
187
 
172
188
  ## View log by node-bunyan
173
189
 
@@ -3,48 +3,49 @@ require 'logger'
3
3
 
4
4
  module Ougai
5
5
  class Logger < ::Logger
6
- attr_accessor :default_message, :exc_key
6
+ attr_accessor :default_message, :exc_key, :with_fields
7
7
 
8
8
  def initialize(*args)
9
9
  super(*args)
10
10
  @default_message = 'No message'
11
11
  @exc_key = :err
12
12
  @formatter = create_formatter
13
+ @with_fields = {}
13
14
  end
14
15
 
15
16
  def debug(message = nil, ex = nil, data = nil, &block)
16
17
  return true if level > DEBUG
17
18
  args = block ? yield : [message, ex, data]
18
- add(DEBUG, to_item(args))
19
+ add(DEBUG, build_log(args))
19
20
  end
20
21
 
21
22
  def info(message = nil, ex = nil, data = nil, &block)
22
23
  return true if level > INFO
23
24
  args = block ? yield : [message, ex, data]
24
- add(INFO, to_item(args))
25
+ add(INFO, build_log(args))
25
26
  end
26
27
 
27
28
  def warn(message = nil, ex = nil, data = nil, &block)
28
29
  return true if level > WARN
29
30
  args = block ? yield : [message, ex, data]
30
- add(WARN, to_item(args))
31
+ add(WARN, build_log(args))
31
32
  end
32
33
 
33
34
  def error(message = nil, ex = nil, data = nil, &block)
34
35
  return true if level > ERROR
35
36
  args = block ? yield : [message, ex, data]
36
- add(ERROR, to_item(args))
37
+ add(ERROR, build_log(args))
37
38
  end
38
39
 
39
40
  def fatal(message = nil, ex = nil, data = nil, &block)
40
41
  return true if level > FATAL
41
42
  args = block ? yield : [message, ex, data]
42
- add(FATAL, to_item(args))
43
+ add(FATAL, build_log(args))
43
44
  end
44
45
 
45
46
  def unknown(message = nil, ex = nil, data = nil, &block)
46
47
  args = block ? yield : [message, ex, data]
47
- add(UNKNOWN, to_item(args))
48
+ add(UNKNOWN, build_log(args))
48
49
  end
49
50
 
50
51
  def self.broadcast(logger)
@@ -68,6 +69,10 @@ module Ougai
68
69
 
69
70
  private
70
71
 
72
+ def build_log(args)
73
+ @with_fields.merge(to_item(args))
74
+ end
75
+
71
76
  def to_item(args)
72
77
  msg, ex, data = args
73
78
 
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "0.9.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -96,6 +96,20 @@ describe Ougai::Logger do
96
96
  expect(item).to be_log_message(log_msg, log_level)
97
97
  expect(item).to include_data(data_id: 108, action: 'dump')
98
98
  end
99
+
100
+ it 'outputs valid with fields' do
101
+ logger.with_fields = { coreField1: 123, core_field2: 'core', 'core_field3' => 456 }
102
+ logger.send(method, { msg: log_msg, data_id: 109, action: 'do' })
103
+ expect(item).to be_log_message(log_msg, log_level)
104
+ expect(item).to include_data(data_id: 109, action: 'do', coreField1: 123, core_field2: 'core', core_field3: 456)
105
+ end
106
+
107
+ it 'outputs valid with fields overridden' do
108
+ logger.with_fields = { core_field1: 'original', core_field2: 'original', err: 'original' }
109
+ logger.send(method, { msg: log_msg, data_id: 110, action: 'do', core_field1: 'override' })
110
+ expect(item).to be_log_message(log_msg, log_level)
111
+ expect(item).to include_data(data_id: 110, action: 'do', core_field1: 'override', core_field2: 'original', err: 'original')
112
+ end
99
113
  end
100
114
 
101
115
  context 'with data that does not contain msg' do
@@ -150,6 +164,17 @@ describe Ougai::Logger do
150
164
  expect(item).to be_log_message(log_msg, log_level)
151
165
  expect(item).to include_error('errmsg')
152
166
  end
167
+
168
+ it 'outputs valid overridden err field' do
169
+ logger.with_fields = { err: 'original' }
170
+ begin
171
+ raise StandardError, 'errmsg2'
172
+ rescue => ex
173
+ logger.send(method, log_msg, ex)
174
+ end
175
+
176
+ expect(item).to include_error('errmsg2')
177
+ end
153
178
  end
154
179
 
155
180
  context 'with exception and data' do
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: 0.9.0
4
+ version: 1.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: 2017-07-01 00:00:00.000000000 Z
11
+ date: 2017-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler