ougai 0.7.4 → 0.8.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: 607cde7c3045e9fcbd0ca91cf1a6ad91bade32cb
4
- data.tar.gz: ddbc3d91e07a00c197ef231ef47e3544effc47b4
3
+ metadata.gz: 234c84c3f7a6eaf7d2dd0ab32c55d4ffa8afda98
4
+ data.tar.gz: 98776af529bf24129daa4826d19d490946fea410
5
5
  SHA512:
6
- metadata.gz: 8da96a24f57a2ce240292a51d19e63d1595d294ca9139fd6846e413c0d08ef60167d43952dc6987e6544aaedbdbae5d0a1951a1a35948fc5c2d8f419dd680ba5
7
- data.tar.gz: dc281f50f0543c07a843dce4064510b153fc60c4823dbcc227b4deaa5f60869333de102abeab10176e169f3ec755daf53b20236bac7fc96bc8e37a1cb0a8c857
6
+ metadata.gz: 370dfb4b6d00241c264f29832347ec9184ce453f2287cc62392cbd8338d0110e0676da2b42ae48fe4c97e1a0f4ed8ee4433b13eb056e566354d41b9766e71925
7
+ data.tar.gz: 637d1b0e58e79d4531b2981728cd8f4109f64b7a8e28ee188fa34f31b11f51e3d86e7abbff3c66d38900752801addde63a0b3f0d07891bfaf9c9befe1d8c43f7
data/.travis.yml CHANGED
@@ -2,4 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1.0
4
4
  - 2.2.3
5
+ - 2.3.4
6
+ - 2.4.1
5
7
  before_install: gem install bundler -v 1.11.2
data/README.md CHANGED
@@ -144,6 +144,28 @@ end
144
144
  {"name":"main","hostname":"mint","pid":14607,"level":50,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Caught error","err":{"name":"ZeroDivisionError","message":"divided by 0","stack":"main.rb:18:in `/'\n ...'"},"reason":"zero spec"}
145
145
  ```
146
146
 
147
+ ### logs with blocks
148
+
149
+ ```ruby
150
+ logger.info { 'Hello!' }
151
+
152
+ logger.debug do
153
+ ['User dump', { name: 'Taro', age: 15 }]
154
+ end
155
+
156
+ logger.error do
157
+ ['Failed to fetch info', ex, { id: 10 }]
158
+ end
159
+
160
+ loggger.fatal { ex }
161
+
162
+ loggger.fatal do
163
+ ['Unexpected', ex]
164
+ end
165
+ ```
166
+
167
+ To specify more than one of a message, an exception and custom data, the block returns them as an array.
168
+
147
169
 
148
170
  ## View log by node-bunyan
149
171
 
@@ -197,6 +219,33 @@ logger.formatter = Ougai::Formatters::Readable.new
197
219
 
198
220
  ## Use on Rails
199
221
 
222
+ ### Define a custom logger
223
+
224
+ Add following code to `lib/your_app/logger.rb`
225
+ A custom logger includes LoggerSilence because Rails logger must support `silence` feature.
226
+
227
+ ```ruby
228
+ module YourApp
229
+ class Logger < Ougai::Logger
230
+ include ActiveSupport::LoggerThreadSafeLevel
231
+ include LoggerSilence
232
+
233
+ def initialize(*args)
234
+ super
235
+ after_initialize if respond_to? :after_initialize
236
+ end
237
+
238
+ def create_formatter
239
+ if Rails.env.development? || Rails.env.test?
240
+ Ougai::Formatters::Readable.new
241
+ else
242
+ Ougai::Formatters::Bunyan.new
243
+ end
244
+ end
245
+ end
246
+ end
247
+ ```
248
+
200
249
  ### for Development
201
250
 
202
251
  Add following code to `config/environments/development.rb`
@@ -205,9 +254,7 @@ Add following code to `config/environments/development.rb`
205
254
  Rails.application.configure do
206
255
  ...
207
256
 
208
- logger = Ougai::Logger.new(STDOUT)
209
- logger.formatter = Ougai::Formatters::Readable.new
210
- config.logger = logger
257
+ config.logger = YourApp::Logger.new(STDOUT)
211
258
  end
212
259
  ```
213
260
 
@@ -220,9 +267,9 @@ Rails.application.configure do
220
267
  ...
221
268
 
222
269
  if ENV["RAILS_LOG_TO_STDOUT"].present?
223
- config.logger = Ougai::Logger.new(STDOUT)
270
+ config.logger = YourApp::Logger.new(STDOUT)
224
271
  else
225
- config.logger = Ougai::Logger.new(config.paths['log'].first)
272
+ config.logger = YourApp::Logger.new(config.paths['log'].first)
226
273
  end
227
274
  end
228
275
  ```
data/lib/ougai/logger.rb CHANGED
@@ -12,24 +12,53 @@ module Ougai
12
12
  @formatter = create_formatter
13
13
  end
14
14
 
15
- def debug(message, ex = nil, data = nil)
16
- super(to_item(message, ex, data))
15
+ def debug(message = nil, ex = nil, data = nil, &block)
16
+ return true if level > DEBUG
17
+ if block_given?
18
+ args = yield
19
+ else
20
+ args = [message, ex, data]
21
+ end
22
+ super(to_item(args), &nil)
17
23
  end
18
24
 
19
- def info(message, ex = nil, data = nil)
20
- super(to_item(message, ex, data))
25
+ def info(message = nil, ex = nil, data = nil)
26
+ return true if level > INFO
27
+ if block_given?
28
+ args = yield
29
+ else
30
+ args = [message, ex, data]
31
+ end
32
+ super(to_item(args), &nil)
21
33
  end
22
34
 
23
- def warn(message, ex = nil, data = nil)
24
- super(to_item(message, ex, data))
35
+ def warn(message = nil, ex = nil, data = nil)
36
+ return true if level > WARN
37
+ if block_given?
38
+ args = yield
39
+ else
40
+ args = [message, ex, data]
41
+ end
42
+ super(to_item(args), &nil)
25
43
  end
26
44
 
27
- def error(message, ex = nil, data = nil)
28
- super(to_item(message, ex, data))
45
+ def error(message = nil, ex = nil, data = nil)
46
+ return true if level > ERROR
47
+ if block_given?
48
+ args = yield
49
+ else
50
+ args = [message, ex, data]
51
+ end
52
+ super(to_item(args), &nil)
29
53
  end
30
54
 
31
- def fatal(message, ex = nil, data = nil)
32
- super(to_item(message, ex, data))
55
+ def fatal(message = nil, ex = nil, data = nil)
56
+ if block_given?
57
+ args = yield
58
+ else
59
+ args = [message, ex, data]
60
+ end
61
+ super(to_item(args), &nil)
33
62
  end
34
63
 
35
64
  def self.broadcast(logger)
@@ -53,7 +82,9 @@ module Ougai
53
82
 
54
83
  private
55
84
 
56
- def to_item(msg, ex, data)
85
+ def to_item(args)
86
+ msg, ex, data = args
87
+
57
88
  item = {}
58
89
  if ex.nil? # 1 arg
59
90
  if msg.is_a?(Exception)
data/lib/ougai/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "0.7.4"
2
+ VERSION = "0.8.0"
3
3
  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: 0.7.4
4
+ version: 0.8.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-05-17 00:00:00.000000000 Z
11
+ date: 2017-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler