ougai 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/lib/ougai.rb +36 -21
  4. data/lib/ougai/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b0465e90dbafc2f753aa9913eaf7e7b3201af61
4
- data.tar.gz: c1a31078a887b752c8a345b205dae87524457e0a
3
+ metadata.gz: 60ba5446b81c3f2211ccdaf9d4e9e3fb4a5eccf3
4
+ data.tar.gz: e2ec9ab27c885466024b65920c324d4bb53cce3d
5
5
  SHA512:
6
- metadata.gz: d8714ecf3b6a701a498ef18d1c224b2897a98513b9d4ff59921c845ec2040dccee448af9b2579d32446577d02f26b41bb21fb29ab7f90f834733cd88cd522d2b
7
- data.tar.gz: 36dfd041ba4a4485b049d67babb112f90f5383e25ac6bb3f114681d2beb14e696fafdad6f9e67c272477f8bafc9ba7a3fea617036fbfea0ee08f90ce9b88da22
6
+ metadata.gz: 5247253014f31ab80f35f6cba271c5ae9e9aa85050262a7e9da66c7c7c0e778c970278bfa9dd0d533ab7e9b9170a0f7e8097eb0cacbc93befe589d0bcb3bed89
7
+ data.tar.gz: 6b9bc32d51eaa2dd33ca74c7c851bf0cec3e594983de5896b0a515914f78e25e161fc9576d4b23b1627c5afb50e98acaa3cb8f4e59a60de8a893eb1315b7df0e
data/README.md CHANGED
@@ -114,6 +114,20 @@ end
114
114
  {"name":"main","hostname":"mint","pid":14607,"level":60,"time":"2016-10-16T22:26:48.836+09:00","v":0,"msg":"Unexpected!","err":{"name":"StandardError","message":"fatal error","stack":"main.rb:12:in `<main>'"}}
115
115
  ```
116
116
 
117
+ ### log with an exception and custom data
118
+
119
+ ```ruby
120
+ begin
121
+ raise StandardError, 'some error'
122
+ rescue => ex
123
+ logger.error(ex, error_id: 999)
124
+ end
125
+ ```
126
+
127
+ ```json
128
+ {"name":"main","hostname":"mint","pid":13962,"level":50,"time":"2016-10-28T23:44:52.144+09:00","v":0,"error_id":999,"err":{"name":"StandardError","message":"some error","stack":"main.rb:40:in `<main>'"}}
129
+ ```
130
+
117
131
  ### log with a message, an exception and custom data
118
132
 
119
133
  ```ruby
data/lib/ougai.rb CHANGED
@@ -7,32 +7,35 @@ require 'json'
7
7
  module Ougai
8
8
  class Logger < Logger
9
9
  attr_accessor :default_message, :app_name
10
+ attr_accessor :ex_key, :ex_trace_indent
10
11
 
11
12
  def initialize(*args)
12
13
  super(*args)
13
14
  @default_message = 'No message'
14
15
  @app_name = File.basename($0, ".rb")
15
16
  @hostname = Socket.gethostname
17
+ @ex_key = :err
18
+ @ex_trace_indent = 2
16
19
  @formatter = create_formatter
17
20
  end
18
21
 
19
- def debug(message, ex = nil, data = {})
22
+ def debug(message, ex = nil, data = nil)
20
23
  super(to_item(message, ex, data))
21
24
  end
22
25
 
23
- def info(message, ex = nil, data = {})
26
+ def info(message, ex = nil, data = nil)
24
27
  super(to_item(message, ex, data))
25
28
  end
26
29
 
27
- def warn(message, ex = nil, data = {})
30
+ def warn(message, ex = nil, data = nil)
28
31
  super(to_item(message, ex, data))
29
32
  end
30
33
 
31
- def error(message, ex = nil, data = {})
34
+ def error(message, ex = nil, data = nil)
32
35
  super(to_item(message, ex, data))
33
36
  end
34
37
 
35
- def fatal(message, ex = nil, data = {})
38
+ def fatal(message, ex = nil, data = nil)
36
39
  super(to_item(message, ex, data))
37
40
  end
38
41
 
@@ -55,23 +58,34 @@ module Ougai
55
58
 
56
59
  def to_item(msg, ex, data)
57
60
  item = {}
58
- if ex.nil? && msg.is_a?(Exception)
59
- item[:msg] = msg.to_s
60
- item[:err] = serialize_ex(msg)
61
- elsif ex
62
- item[:msg] = msg
63
- if ex.is_a?(Hash)
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)
64
76
  item.merge!(ex)
65
- elsif ex.is_a?(Exception)
66
- item[:err] = serialize_ex(ex)
67
- item.merge!(data)
77
+ if msg.is_a?(Exception)
78
+ item[@ex_key] = serialize_ex(msg)
79
+ else
80
+ item[:msg] = msg.to_s
81
+ end
68
82
  end
69
- elsif msg.is_a?(Hash)
70
- item[:msg] = @default_message unless msg.key?(:msg)
71
- item.merge!(msg)
72
- else
73
- item[:msg] = msg
74
- item.merge!(data)
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
75
89
  end
76
90
  item
77
91
  end
@@ -82,7 +96,8 @@ module Ougai
82
96
  message: ex.to_s
83
97
  }
84
98
  if ex.backtrace
85
- err[:stack] = ex.backtrace.join("\n ")
99
+ sp = "\n" + ' ' * @ex_trace_indent
100
+ err[:stack] = ex.backtrace.join(sp)
86
101
  end
87
102
  err
88
103
  end
data/lib/ougai/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ougai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshimitsu Takahashi