ougai 0.1.2 → 0.2.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 +27 -0
  3. data/lib/ougai.rb +24 -12
  4. data/lib/ougai/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 908df02d45d84049df967249b72ad554ad0859ee
4
- data.tar.gz: 3b99828ad56c2d6ca951e6098704793c55440188
3
+ metadata.gz: 6b0465e90dbafc2f753aa9913eaf7e7b3201af61
4
+ data.tar.gz: c1a31078a887b752c8a345b205dae87524457e0a
5
5
  SHA512:
6
- metadata.gz: c6eb4308b0825f963103e3247f8d1a4fb6bfe74f2b0b78451a73b1c62ab2950857571c8e5c108c067242925ed2209a130e807dde9c61e1427ebb882615507a50
7
- data.tar.gz: 36df6974f50e6c4d1100c20a09f34810335703bce814941f3f9643869ea4c1937c82690ef4c55f9906ece2575ee1f1d38f4eeab700479da90c016a5a9db08326
6
+ metadata.gz: d8714ecf3b6a701a498ef18d1c224b2897a98513b9d4ff59921c845ec2040dccee448af9b2579d32446577d02f26b41bb21fb29ab7f90f834733cd88cd522d2b
7
+ data.tar.gz: 36dfd041ba4a4485b049d67babb112f90f5383e25ac6bb3f114681d2beb14e696fafdad6f9e67c272477f8bafc9ba7a3fea617036fbfea0ee08f90ce9b88da22
data/README.md CHANGED
@@ -47,6 +47,33 @@ logger.info('Information!')
47
47
  {"name":"main","hostname":"mint","pid":14607,"level":30,"time":"2016-10-16T22:26:48.835+09:00","v":0,"msg":"Information!"}
48
48
  ```
49
49
 
50
+ ### log only a data
51
+
52
+ ```ruby
53
+ logger.info({
54
+ msg: 'Request', method: 'GET', path: '/login',
55
+ format: 'html', controller: 'LoginController',
56
+ action: 'new', status: 200
57
+ })
58
+ logger.debug(user: { name: 'Taro', age: 19 })
59
+ ```
60
+
61
+ ```json
62
+ {"name":"main","hostname":"mint","pid":9044,"level":30,"time":"2016-10-28T17:58:53.668+09:00","v":0,"msg":"Request","method":"GET","path":"/login","format":"html","controller":"LoginController","action":"new","status":200}
63
+ {"name":"main","hostname":"mint","pid":9044,"level":20,"time":"2016-10-28T17:58:53.668+09:00","v":0,"msg":"No message","user":{"name":"Taro","age":19}}
64
+ ```
65
+
66
+ If a data does not contain `msg` field, msg is set `default_message` attribute value of a Logger. its default is 'No message'.
67
+
68
+ ```ruby
69
+ logger.default_message = 'User dump'
70
+ logger.debug(user: { name: 'Taro', age: 19 })
71
+ ```
72
+
73
+ ```json
74
+ {"name":"main","hostname":"mint","pid":9303,"level":20,"time":"2016-10-28T18:03:50.118+09:00","v":0,"msg":"User dump","user":{"name":"Taro","age":19}}
75
+ ```
76
+
50
77
  ### log only an exception
51
78
 
52
79
  ```ruby
@@ -6,20 +6,14 @@ require 'json'
6
6
 
7
7
  module Ougai
8
8
  class Logger < Logger
9
+ attr_accessor :default_message, :app_name
10
+
9
11
  def initialize(*args)
10
12
  super(*args)
11
- run_filename = File.basename($0, ".rb")
12
- hostname = Socket.gethostname
13
- @formatter = proc do |severity, time, progname, data|
14
- JSON.generate({
15
- name: progname || run_filename,
16
- hostname: hostname,
17
- pid: $$,
18
- level: to_level(severity),
19
- time: time.iso8601(3),
20
- v: 0
21
- }.merge(data)) + "\n"
22
- end
13
+ @default_message = 'No message'
14
+ @app_name = File.basename($0, ".rb")
15
+ @hostname = Socket.gethostname
16
+ @formatter = create_formatter
23
17
  end
24
18
 
25
19
  def debug(message, ex = nil, data = {})
@@ -42,6 +36,21 @@ module Ougai
42
36
  super(to_item(message, ex, data))
43
37
  end
44
38
 
39
+ protected
40
+
41
+ def create_formatter
42
+ proc do |severity, time, progname, data|
43
+ JSON.generate({
44
+ name: progname || @app_name,
45
+ hostname: @hostname,
46
+ pid: $$,
47
+ level: to_level(severity),
48
+ time: time.iso8601(3),
49
+ v: 0
50
+ }.merge(data)) + "\n"
51
+ end
52
+ end
53
+
45
54
  private
46
55
 
47
56
  def to_item(msg, ex, data)
@@ -57,6 +66,9 @@ module Ougai
57
66
  item[:err] = serialize_ex(ex)
58
67
  item.merge!(data)
59
68
  end
69
+ elsif msg.is_a?(Hash)
70
+ item[:msg] = @default_message unless msg.key?(:msg)
71
+ item.merge!(msg)
60
72
  else
61
73
  item[:msg] = msg
62
74
  item.merge!(data)
@@ -1,3 +1,3 @@
1
1
  module Ougai
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.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.1.2
4
+ version: 0.2.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-22 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler