ougai 1.4.1 → 1.4.2

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: 8386aa7b0737156ca211cfdb4cc7c4030751dc2a
4
- data.tar.gz: 9fde65ba8e677511d6ad7061e65dc5816c52bced
3
+ metadata.gz: fd24ccdad4b3e8fa0a273e6bc4d4a7633b8a2099
4
+ data.tar.gz: b4ac8f5e868affc6c44e5592651f68ca634ae3ba
5
5
  SHA512:
6
- metadata.gz: b75ed0bd57138091a7990179072355120bd81fd4f22314154c0c86878ad9372937550c1a2e3dede39858bc8f0c564a8ad13589dcb3284aa67f205625ad7df82d
7
- data.tar.gz: dcd961d585d741efd7134bb94ccfca3e981429073b03eac0e6ccfad8e9bdd5c906c2e1f27a68f9467abcdf8407a4d91f20cabaccb0305e0963ef227c0e41ebc3
6
+ metadata.gz: bf27f89b20a9bd241fd329b756be6d1d5cd163bae87dd0c7f51dc8e3b95f6c58dc857fb6e4765c16a1162279465fe2d21378af45323f56acf1e8d02f01028799
7
+ data.tar.gz: def31d8b2f563ab7e12e9fea200796177f417b9e3523ee9890c5edb60a647826e477e9c2db412a06b285921e457ddc81cedf516fe86ea500758e53900411a357
data/README.md CHANGED
@@ -6,7 +6,7 @@ Ougai
6
6
  [![Code Climate](https://codeclimate.com/github/tilfin/ougai/badges/gpa.svg)](https://codeclimate.com/github/tilfin/ougai)
7
7
  [![Test Coverage](https://codeclimate.com/github/tilfin/ougai/badges/coverage.svg)](https://codeclimate.com/github/tilfin/ougai/coverage)
8
8
 
9
- A JSON logging system is capable of handling a message, data or an exception easily.
9
+ A structured JSON logging system is capable of handling a message, structured data or an exception easily.
10
10
  It is compatible with [Bunyan](https://github.com/trentm/node-bunyan) for Node.js.
11
11
  It can also output human readable format for the console.
12
12
 
@@ -327,79 +327,6 @@ logger.formatter = Ougai::Formatters::Readable.new
327
327
  ![Screen Shot](https://github.com/tilfin/ougai/blob/images/ougai_readable_format.png?raw=true)
328
328
 
329
329
 
330
- ## Use on Rails
331
-
332
- ### Define a custom logger
333
-
334
- Add following code to `lib/your_app/logger.rb`
335
- A custom logger includes LoggerSilence because Rails logger must support `silence` feature.
336
-
337
- ```ruby
338
- module YourApp
339
- class Logger < Ougai::Logger
340
- include ActiveSupport::LoggerThreadSafeLevel
341
- include LoggerSilence
342
-
343
- def initialize(*args)
344
- super
345
- after_initialize if respond_to? :after_initialize
346
- end
347
-
348
- def create_formatter
349
- if Rails.env.development? || Rails.env.test?
350
- Ougai::Formatters::Readable.new
351
- else
352
- Ougai::Formatters::Bunyan.new
353
- end
354
- end
355
- end
356
- end
357
- ```
358
-
359
- ### for Development
360
-
361
- Add following code to `config/environments/development.rb`
362
-
363
- ```ruby
364
- Rails.application.configure do
365
- ...
366
-
367
- config.logger = YourApp::Logger.new(STDOUT)
368
- end
369
- ```
370
-
371
- ### for Production
372
-
373
- Add following code to the end block of `config/environments/production.rb`
374
-
375
- ```ruby
376
- Rails.application.configure do
377
- ...
378
-
379
- if ENV["RAILS_LOG_TO_STDOUT"].present?
380
- config.logger = YourApp::Logger.new(STDOUT)
381
- else
382
- config.logger = YourApp::Logger.new(config.paths['log'].first)
383
- end
384
- end
385
- ```
386
-
387
- ### With Lograge
388
-
389
- You must modify [lograge](https://github.com/roidrage/lograge) formatter like *Raw*.
390
- The following code set request data to `request` field of JSON.
391
-
392
- ```ruby
393
- Rails.application.configure do
394
- config.lograge.enabled = true
395
- config.lograge.formatter = Class.new do |fmt|
396
- def fmt.call(data)
397
- { msg: 'Request', request: data }
398
- end
399
- end
400
- end
401
- ```
402
-
403
330
  ### Output example on development
404
331
 
405
332
  If you modify `application_controller.rb` as
@@ -438,6 +365,11 @@ logger outputs
438
365
  }
439
366
  ```
440
367
 
368
+ ## How to use with famous products and libraries
369
+
370
+ - [Use as Rails logger](https://github.com/tilfin/ougai/wiki/Use-as-Rails-logger)
371
+ - [Customize Sidekiq logger](https://github.com/tilfin/ougai/wiki/Customize-Sidekiq-logger)
372
+
441
373
  ## License
442
374
 
443
375
  [MIT](LICENSE.txt)
@@ -4,15 +4,22 @@ require 'json'
4
4
  module Ougai
5
5
  module Formatters
6
6
  class Bunyan < Base
7
+ attr_accessor :jsonize
8
+
9
+ def initialize
10
+ super
11
+ @jsonize = true
12
+ end
13
+
7
14
  def call(severity, time, progname, data)
8
- JSON.generate({
15
+ dump({
9
16
  name: progname || @app_name,
10
17
  hostname: @hostname,
11
18
  pid: $$,
12
19
  level: to_level(severity),
13
- time: time.iso8601(3),
20
+ time: time,
14
21
  v: 0
15
- }.merge(data)) + "\n"
22
+ }.merge(data))
16
23
  end
17
24
 
18
25
  def to_level(severity)
@@ -31,6 +38,14 @@ module Ougai
31
38
  20
32
39
  end
33
40
  end
41
+
42
+ private
43
+
44
+ def dump(data)
45
+ return data unless @jsonize
46
+ data[:time] = data[:time].iso8601(3)
47
+ JSON.generate(data) + "\n"
48
+ end
34
49
  end
35
50
  end
36
51
  end
data/lib/ougai/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "1.4.1"
2
+ VERSION = "1.4.2"
3
3
  end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ougai::Formatters::Bunyan do
4
+ let(:data) do
5
+ {
6
+ msg: 'Log Message!',
7
+ status: 200,
8
+ method: 'GET',
9
+ path: '/'
10
+ }
11
+ end
12
+
13
+ let(:err) do
14
+ {
15
+ name: 'DummyError',
16
+ message: 'it is dummy.',
17
+ stack: "error1.rb\n error2.rb"
18
+ }
19
+ end
20
+
21
+ let(:formatter) { described_class.new }
22
+
23
+ context 'when severity is DEBUG' do
24
+ subject { JSON.parse(formatter.call('DEBUG', Time.now, nil, data).chomp, symbolize_names: true) }
25
+
26
+ it 'includes valid strings' do
27
+ expect(subject).to include(data.merge(level: 20))
28
+ expect(subject[:time]).to match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/)
29
+ end
30
+ end
31
+
32
+ context 'jsonize is falsey' do
33
+ before do
34
+ formatter.jsonize = false
35
+ end
36
+
37
+ context 'when severity is DEBUG' do
38
+ subject { formatter.call('DEBUG', Time.now, nil, data) }
39
+
40
+ it 'includes valid hash' do
41
+ expect(subject).to include(data.merge(level: 20))
42
+ expect(subject[:time]).to be_an_instance_of(Time)
43
+ end
44
+ end
45
+
46
+ context 'when severity is INFO' do
47
+ subject { formatter.call('INFO', Time.now, nil, data) }
48
+
49
+ it 'includes valid hash' do
50
+ expect(subject).to include(data.merge(level: 30))
51
+ expect(subject[:time]).to be_an_instance_of(Time)
52
+ end
53
+ end
54
+
55
+ context 'when severity is WARN' do
56
+ subject { formatter.call('WARN', Time.now, nil, data) }
57
+
58
+ it 'includes valid hash' do
59
+ expect(subject).to include(data.merge(level: 40))
60
+ expect(subject[:time]).to be_an_instance_of(Time)
61
+ end
62
+ end
63
+
64
+ context 'when severity is ERROR' do
65
+ subject { formatter.call('ERROR', Time.now, nil, data.merge({ err: err })) }
66
+
67
+ it 'includes valid hash' do
68
+ expect(subject).to include(level: 50, err: err)
69
+ expect(subject[:time]).to be_an_instance_of(Time)
70
+ end
71
+ end
72
+
73
+ context 'when severity is FATAL' do
74
+ subject { formatter.call('FATAL', Time.now, nil, { msg: 'TheEnd', err: err }) }
75
+
76
+ it 'includes valid hash' do
77
+ expect(subject).to include(level: 60, err: err)
78
+ expect(subject[:time]).to be_an_instance_of(Time)
79
+ end
80
+ end
81
+ end
82
+ 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.4.1
4
+ version: 1.4.2
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-08-16 00:00:00.000000000 Z
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,7 @@ files:
77
77
  - lib/ougai/version.rb
78
78
  - spec/child_logger_spec.rb
79
79
  - spec/formatters/base_spec.rb
80
+ - spec/formatters/bunyan_spec.rb
80
81
  - spec/formatters/readable_spec.rb
81
82
  - spec/logger_spec.rb
82
83
  - spec/logging_spec.rb
@@ -109,6 +110,7 @@ summary: JSON logger compatible with node-bunyan is capable of handling data eas
109
110
  test_files:
110
111
  - spec/child_logger_spec.rb
111
112
  - spec/formatters/base_spec.rb
113
+ - spec/formatters/bunyan_spec.rb
112
114
  - spec/formatters/readable_spec.rb
113
115
  - spec/logger_spec.rb
114
116
  - spec/logging_spec.rb