ougai 0.3.0 → 0.7.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: 60ba5446b81c3f2211ccdaf9d4e9e3fb4a5eccf3
4
- data.tar.gz: e2ec9ab27c885466024b65920c324d4bb53cce3d
3
+ metadata.gz: db633bee49261c5ccbd935510e55a44128dc751c
4
+ data.tar.gz: 4525a5d1f6785e7a92240928eb986a17c7a1c06f
5
5
  SHA512:
6
- metadata.gz: 5247253014f31ab80f35f6cba271c5ae9e9aa85050262a7e9da66c7c7c0e778c970278bfa9dd0d533ab7e9b9170a0f7e8097eb0cacbc93befe589d0bcb3bed89
7
- data.tar.gz: 6b9bc32d51eaa2dd33ca74c7c851bf0cec3e594983de5896b0a515914f78e25e161fc9576d4b23b1627c5afb50e98acaa3cb8f4e59a60de8a893eb1315b7df0e
6
+ metadata.gz: 2c9635f9cca06b4324cf0796a62069d26f37dcba4dd00f81e897b9d1bb6d8af710221993bafbbda1cb4119ed2df977cc8bafe475c7951a207d7893b0a6cdb5a5
7
+ data.tar.gz: c4dd71d91605a37dcbe28c64f1086a5db346d642e2acbc23ec31d1bf07724bf016eab4a3d792f72ca6f709402ba871ce29afb6e9df7b2a0d7fa0890840370d0b
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ougai.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'awesome_print'
8
+ end
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
1
  Ougai
2
2
  =====
3
3
 
4
- [![Build Status](https://travis-ci.org/tilfin/ougai.svg?branch=master)](https://travis-ci.org/tilfin/ougai)
5
4
  [![Gem Version](https://badge.fury.io/rb/ougai.svg)](https://badge.fury.io/rb/ougai)
5
+ [![Build Status](https://travis-ci.org/tilfin/ougai.svg?branch=master)](https://travis-ci.org/tilfin/ougai)
6
6
 
7
- A JSON logger is compatible with [bunyan](https://github.com/trentm/node-bunyan) for Node.js
7
+ A JSON logger is capable of handling a message, data or an exception easily.
8
+ It is compatible with [bunyan](https://github.com/trentm/node-bunyan) for Node.js.
9
+ It can also output human readable format for the console.
8
10
 
9
11
  ## Installation
10
12
 
@@ -143,7 +145,7 @@ end
143
145
  ```
144
146
 
145
147
 
146
- ## View log by node-bunyan
148
+ ### View log by node-bunyan
147
149
 
148
150
  Install [bunyan](https://github.com/trentm/node-bunyan) via NPM
149
151
 
@@ -169,6 +171,27 @@ $ bunyan output.log
169
171
  main.rb:18:in `<main>'
170
172
  ```
171
173
 
174
+ ## Use human Readable formatter for console
175
+
176
+ Add awesome_print to Gemfile and `bundle`
177
+
178
+ ```ruby
179
+ gem 'awesome_print'
180
+ ```
181
+
182
+ Set *Ougai::Formatters::Readable* instance to `formatter` accessor
183
+
184
+ ```ruby
185
+ require 'rubygems'
186
+ require 'ougai'
187
+
188
+ logger = Ougai::Logger.new(STDOUT)
189
+ logger.formatter = Ougai::Formatters::Readable.new
190
+ ```
191
+
192
+ ### Screen result example
193
+
194
+ ![Screen Shot](https://github.com/tilfin/ougai/blob/images/ougai_readable_format.png?raw=true))
172
195
 
173
196
  ## License
174
197
 
data/lib/ougai.rb CHANGED
@@ -1,120 +1,5 @@
1
- require "ougai/version"
2
- require 'logger'
3
- require 'socket'
4
- require 'time'
5
- require 'json'
6
-
7
- module Ougai
8
- class Logger < Logger
9
- attr_accessor :default_message, :app_name
10
- attr_accessor :ex_key, :ex_trace_indent
11
-
12
- def initialize(*args)
13
- super(*args)
14
- @default_message = 'No message'
15
- @app_name = File.basename($0, ".rb")
16
- @hostname = Socket.gethostname
17
- @ex_key = :err
18
- @ex_trace_indent = 2
19
- @formatter = create_formatter
20
- end
21
-
22
- def debug(message, ex = nil, data = nil)
23
- super(to_item(message, ex, data))
24
- end
25
-
26
- def info(message, ex = nil, data = nil)
27
- super(to_item(message, ex, data))
28
- end
29
-
30
- def warn(message, ex = nil, data = nil)
31
- super(to_item(message, ex, data))
32
- end
33
-
34
- def error(message, ex = nil, data = nil)
35
- super(to_item(message, ex, data))
36
- end
37
-
38
- def fatal(message, ex = nil, data = nil)
39
- super(to_item(message, ex, data))
40
- end
41
-
42
- protected
43
-
44
- def create_formatter
45
- proc do |severity, time, progname, data|
46
- JSON.generate({
47
- name: progname || @app_name,
48
- hostname: @hostname,
49
- pid: $$,
50
- level: to_level(severity),
51
- time: time.iso8601(3),
52
- v: 0
53
- }.merge(data)) + "\n"
54
- end
55
- end
56
-
57
- private
58
-
59
- def to_item(msg, ex, data)
60
- item = {}
61
- if ex.nil? # 1 arg
62
- if msg.is_a?(Exception)
63
- item[:msg] = msg.to_s
64
- item[@ex_key] = serialize_ex(msg)
65
- elsif msg.is_a?(Hash)
66
- item[:msg] = @default_message unless msg.key?(:msg)
67
- item.merge!(msg)
68
- else
69
- item[:msg] = msg.to_s
70
- end
71
- elsif data.nil? # 2 args
72
- if ex.is_a?(Exception)
73
- item[:msg] = msg.to_s
74
- item[@ex_key] = serialize_ex(ex)
75
- elsif ex.is_a?(Hash)
76
- item.merge!(ex)
77
- if msg.is_a?(Exception)
78
- item[@ex_key] = serialize_ex(msg)
79
- else
80
- item[:msg] = msg.to_s
81
- end
82
- end
83
- elsif msg # 3 args
84
- item[@ex_key] = serialize_ex(ex) if ex.is_a?(Exception)
85
- item.merge!(data) if data.is_a?(Hash)
86
- item[:msg] = msg.to_s
87
- else # No args
88
- item[:msg] = @default_message
89
- end
90
- item
91
- end
92
-
93
- def serialize_ex(ex)
94
- err = {
95
- name: ex.class.name,
96
- message: ex.to_s
97
- }
98
- if ex.backtrace
99
- sp = "\n" + ' ' * @ex_trace_indent
100
- err[:stack] = ex.backtrace.join(sp)
101
- end
102
- err
103
- end
104
-
105
- def to_level(severity)
106
- case severity
107
- when 'INFO'
108
- 30
109
- when 'WARN'
110
- 40
111
- when 'ERROR'
112
- 50
113
- when 'FATAL'
114
- 60
115
- else # DEBUG
116
- 20
117
- end
118
- end
119
- end
120
- end
1
+ require 'ougai/version'
2
+ require 'ougai/formatters/base'
3
+ require 'ougai/formatters/bunyan'
4
+ require 'ougai/formatters/readable'
5
+ require 'ougai/logger'
@@ -0,0 +1,34 @@
1
+ require 'logger'
2
+ require 'time'
3
+ require 'socket'
4
+
5
+ module Ougai
6
+ module Formatters
7
+ class Base < Logger::Formatter
8
+ attr_accessor :trace_indent, :trace_max_lines
9
+
10
+ def initialize(app_name = nil, hostname = nil)
11
+ @app_name = app_name || File.basename($0, ".rb")
12
+ @hostname = hostname || Socket.gethostname
13
+ @trace_indent = 2
14
+ @trace_max_lines = 100
15
+ end
16
+
17
+ def serialize_exc(ex)
18
+ err = {
19
+ name: ex.class.name,
20
+ message: ex.to_s
21
+ }
22
+ if ex.backtrace
23
+ err[:stack] = serialize_trace(ex.backtrace)
24
+ end
25
+ err
26
+ end
27
+
28
+ def serialize_trace(trace)
29
+ sp = "\n" + ' ' * @trace_indent
30
+ trace.slice(0, @trace_max_lines).join(sp)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ require 'ougai/formatters/base'
2
+ require 'json'
3
+
4
+ module Ougai
5
+ module Formatters
6
+ class Bunyan < Base
7
+ def call(severity, time, progname, data)
8
+ JSON.generate({
9
+ name: progname || @app_name,
10
+ hostname: @hostname,
11
+ pid: $$,
12
+ level: to_level(severity),
13
+ time: time.iso8601(3),
14
+ v: 0
15
+ }.merge(data)) + "\n"
16
+ end
17
+
18
+ def to_level(severity)
19
+ case severity
20
+ when 'INFO'
21
+ 30
22
+ when 'WARN'
23
+ 40
24
+ when 'ERROR'
25
+ 50
26
+ when 'FATAL'
27
+ 60
28
+ else # DEBUG
29
+ 20
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,53 @@
1
+ require 'ougai/formatters/base'
2
+
3
+ module Ougai
4
+ module Formatters
5
+ class Readable < Base
6
+ def initialize(app_name = nil, hostname = nil)
7
+ super(app_name, hostname)
8
+ @trace_indent = 4
9
+ load_dependency
10
+ end
11
+
12
+ def call(severity, time, progname, data)
13
+ msg = data.delete(:msg)
14
+ strs = ["[#{time.iso8601(3)}] #{colored_level(severity)}: #{msg}"]
15
+ if data.key?(:err)
16
+ err = data.delete(:err)
17
+ err_str = " #{err[:name]} (#{err[:message]}):"
18
+ err_str += "\n " + err[:stack] if err.key?(:stack)
19
+ strs.push(err_str)
20
+ end
21
+ unless data.empty?
22
+ strs.push(data.ai)
23
+ end
24
+ strs.join("\n") + "\n"
25
+ end
26
+
27
+ def colored_level(severity)
28
+ case severity
29
+ when 'INFO'
30
+ color = '0;36'
31
+ when 'WARN'
32
+ color = '0;33'
33
+ when 'ERROR'
34
+ color = '0;31'
35
+ when 'FATAL'
36
+ color = '0;35'
37
+ else # DEBUG
38
+ color = '0;37'
39
+ end
40
+ "\e[#{color}m#{severity}\e[0m"
41
+ end
42
+
43
+ private
44
+
45
+ def load_dependency
46
+ require 'awesome_print'
47
+ rescue LoadError
48
+ puts 'You must install the awesome_print gem to use this output.'
49
+ raise
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,81 @@
1
+ require 'ougai/formatters/bunyan'
2
+ require 'logger'
3
+
4
+ module Ougai
5
+ class Logger < ::Logger
6
+ attr_accessor :default_message, :exc_key
7
+
8
+ def initialize(*args)
9
+ super(*args)
10
+ @default_message = 'No message'
11
+ @exc_key = :err
12
+ @formatter = create_formatter
13
+ end
14
+
15
+ def debug(message, ex = nil, data = nil)
16
+ super(to_item(message, ex, data))
17
+ end
18
+
19
+ def info(message, ex = nil, data = nil)
20
+ super(to_item(message, ex, data))
21
+ end
22
+
23
+ def warn(message, ex = nil, data = nil)
24
+ super(to_item(message, ex, data))
25
+ end
26
+
27
+ def error(message, ex = nil, data = nil)
28
+ super(to_item(message, ex, data))
29
+ end
30
+
31
+ def fatal(message, ex = nil, data = nil)
32
+ super(to_item(message, ex, data))
33
+ end
34
+
35
+ protected
36
+
37
+ def create_formatter
38
+ Formatters::Bunyan.new
39
+ end
40
+
41
+ private
42
+
43
+ def to_item(msg, ex, data)
44
+ item = {}
45
+ if ex.nil? # 1 arg
46
+ if msg.is_a?(Exception)
47
+ item[:msg] = msg.to_s
48
+ set_exc(item, msg)
49
+ elsif msg.is_a?(Hash)
50
+ item[:msg] = @default_message unless msg.key?(:msg)
51
+ item.merge!(msg)
52
+ else
53
+ item[:msg] = msg.to_s
54
+ end
55
+ elsif data.nil? # 2 args
56
+ if ex.is_a?(Exception)
57
+ item[:msg] = msg.to_s
58
+ set_exc(item, ex)
59
+ elsif ex.is_a?(Hash)
60
+ item.merge!(ex)
61
+ if msg.is_a?(Exception)
62
+ set_exc(item, msg)
63
+ else
64
+ item[:msg] = msg.to_s
65
+ end
66
+ end
67
+ elsif msg # 3 args
68
+ set_exc(item, ex) if ex.is_a?(Exception)
69
+ item.merge!(data) if data.is_a?(Hash)
70
+ item[:msg] = msg.to_s
71
+ else # No args
72
+ item[:msg] = @default_message
73
+ end
74
+ item
75
+ end
76
+
77
+ def set_exc(item, exc)
78
+ item[@exc_key] = @formatter.serialize_exc(exc)
79
+ end
80
+ end
81
+ end
data/lib/ougai/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "0.3.0"
2
+ VERSION = "0.7.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.3.0
4
+ version: 0.7.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: 2016-10-28 00:00:00.000000000 Z
11
+ date: 2016-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,10 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - lib/ougai.rb
70
+ - lib/ougai/formatters/base.rb
71
+ - lib/ougai/formatters/bunyan.rb
72
+ - lib/ougai/formatters/readable.rb
73
+ - lib/ougai/logger.rb
70
74
  - lib/ougai/version.rb
71
75
  - ougai.gemspec
72
76
  homepage: https://github.com/tilfin/ougai