log_formatter 0.2.2 → 0.4.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: b97b7413e98089478b1a142dc299f585f0d74c00
4
- data.tar.gz: 5538f461677513e84552095d670bb0a7b8980827
3
+ metadata.gz: 7650cd6a12ef1c0e615e67317578693bfab7f994
4
+ data.tar.gz: 2ce529cbb4a652a195238d48ae48e11bbb549c83
5
5
  SHA512:
6
- metadata.gz: c20e4e83132793352aa24baa58089eaefd97804bf73074b908d7c6bf4fb5e46113ed69c134a48781ff67203858be63b4969a6bcbe58ac25e479b0cd595fc0c9c
7
- data.tar.gz: 1bf2c1f5ce2a08c2269e7b0009b46920c2e83de76cf753df3bccf013438a5ea78d85177d107c0623381c258d620bbb9b1c010043dde14b2788e5717f082e470b
6
+ metadata.gz: bf93c45bce92117e79477600c66ce44cb0248b10291ee00d0273fd516bad8d7d240be5b75285e153162e218e5da99413f4b90e89b69c74f7dea233cc7583d361
7
+ data.tar.gz: 7d2e4dac17e60d3e7e5a98e49df0d60e37e970ad2b3bbf2525733a85014e0b6fcabb24a9ef2430a5b1ef7142784e555fb460d9fb64c73357a1050eb99df47f7d
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # LogFormatter
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/log_formatter.svg)](https://badge.fury.io/rb/log_formatter)
4
+
3
5
  Log Formatter for Ruby and other logger.
4
6
 
5
7
  Details as following:
@@ -30,7 +32,7 @@ Or install it yourself as:
30
32
  ### quick start
31
33
 
32
34
  ```
33
- require 'log_formatter.rb'
35
+ require 'log_formatter'
34
36
  require 'log_formatter/ruby_json_formatter'
35
37
 
36
38
  logger = Logger.new(STDOUT)
@@ -124,6 +126,32 @@ result:
124
126
  }
125
127
  ```
126
128
 
129
+ ### disable auto generate keys
130
+
131
+ json formatter will add `log_type`,`log_level`,`log_timestamp`,`log_app` as default key, but you can remove them if needed by setted to false.
132
+
133
+ ```
134
+ logger.formatter = Ruby::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
135
+ config[:level] = false
136
+ config[:type] = false
137
+ config[:app] = :cus_app
138
+ config[:timestamp] = false
139
+ end
140
+
141
+ logger.debug({data: "test data", age: 18})
142
+ ```
143
+
144
+ result:
145
+
146
+ ```
147
+ {
148
+ "source": "examples",
149
+ "data": "test data",
150
+ age: 18,
151
+ "cus_app": "app"
152
+ }
153
+ ```
154
+
127
155
  full code to see [examples/ruby_logger](https://github.com/chadlwm/log_formatter/blob/master/examples/ruby_logger.rb)
128
156
 
129
157
 
@@ -50,6 +50,7 @@ logger3.warn({data: "Nothing to do!"})
50
50
  # {"source":"examples","data":"Nothing to do!","log_level":"WARN","log_type":"Log4RTest","log_app":"app","log_timestamp":"2016-08-25T17:03:48+08:00"}
51
51
 
52
52
 
53
+ # log with custome key instead of default keys
53
54
  logger4 = Log4r::Logger.new('Log4RTest')
54
55
  json_formatter4 = Log4r::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
55
56
  config[:level] = :cus_level
@@ -69,3 +70,25 @@ logger4.warn({data: "Nothing to do!", autho: 'chad'})
69
70
  # {"source":"examples","data":"Created logger","autho":"chad","cus_level":"DEBUG","cus_type":"Log4RTest","cus_app":"app","cus_timestamp":"2016-08-25T17:06:06+08:00"}
70
71
  # {"source":"examples","data":"Program started","autho":"chad","cus_level":"INFO","cus_type":"Log4RTest","cus_app":"app","cus_timestamp":"2016-08-25T17:06:06+08:00"}
71
72
  # {"source":"examples","data":"Nothing to do!","autho":"chad","cus_level":"WARN","cus_type":"Log4RTest","cus_app":"app","cus_timestamp":"2016-08-25T17:06:06+08:00"}
73
+
74
+
75
+ # log to disable some auto generate keys by setting to fasle
76
+ logger4 = Log4r::Logger.new('Log4RTest')
77
+ json_formatter4 = Log4r::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
78
+ config[:level] = false
79
+ config[:type] = false
80
+ config[:app] = :cus_app
81
+ config[:timestamp] = false
82
+ end
83
+ outputter4 = Log4r::StdoutOutputter.new(
84
+ "console",
85
+ :formatter => json_formatter4
86
+ )
87
+ logger4.add(outputter4)
88
+
89
+ logger4.debug({data: "Created logger", autho: 'chad'})
90
+ logger4.info({data: "Program started", autho: 'chad'})
91
+ logger4.warn({data: "Nothing to do!", autho: 'chad'})
92
+ # {"source":"examples","data":"Created logger","autho":"chad","false":"2016-09-01T23:40:39+08:00","cus_app":"app"}
93
+ # {"source":"examples","data":"Program started","autho":"chad","false":"2016-09-01T23:40:39+08:00","cus_app":"app"}
94
+ # {"source":"examples","data":"Nothing to do!","autho":"chad","false":"2016-09-01T23:40:39+08:00","cus_app":"app"}
@@ -48,4 +48,20 @@ logger.info({data: "Program started"})
48
48
  logger.warn({data: "Nothing to do!"})
49
49
  # {"source":"examples","data":"Created logger","cus_level":"DEBUG","cus_type":null,"cus_app":"app","cus_timestamp":"2016-08-25T15:34:25+08:00"}
50
50
  # {"source":"examples","data":"Program started","cus_level":"INFO","cus_type":null,"cus_app":"app","cus_timestamp":"2016-08-25T15:34:25+08:00"}
51
- # {"source":"examples","data":"Nothing to do!","cus_level":"WARN","cus_type":null,"cus_app":"app","cus_timestamp":"2016-08-25T15:34:25+08:00"}
51
+ # {"source":"examples","data":"Nothing to do!","cus_level":"WARN","cus_type":null,"cus_app":"app","cus_timestamp":"2016-08-25T15:34:25+08:00"}
52
+
53
+
54
+ # log to disable some auto generate keys by setting to fasle
55
+ logger.formatter = Ruby::JSONFormatter::Base.new('app', {'source': 'examples'}) do |config|
56
+ config[:level] = false
57
+ config[:type] = false
58
+ config[:app] = :cus_app
59
+ config[:timestamp] = false
60
+ end
61
+
62
+ logger.debug({data: "Created logger"})
63
+ logger.info({data: "Program started"})
64
+ logger.warn({data: "Nothing to do!"})
65
+ # {"source":"examples","data":"Created logger","false":"2016-09-01T23:38:07+08:00","cus_app":"app"}
66
+ # {"source":"examples","data":"Program started","false":"2016-09-01T23:38:07+08:00","cus_app":"app"}
67
+ # {"source":"examples","data":"Nothing to do!","false":"2016-09-01T23:38:07+08:00","cus_app":"app"}
@@ -12,10 +12,10 @@ module LogFormatter::Common
12
12
  {"message".freeze => msg2str(data)}
13
13
  end
14
14
 
15
- event[@config[:level].freeze] ||= severity
16
- event[@config[:type].freeze] = progname
17
- event[@config[:app].freeze] = @app
18
- event[@config[:timestamp].freeze] = current_time(time).iso8601
15
+ event[@config[:level].freeze] ||= severity if @config[:level]
16
+ event[@config[:type].freeze] = progname if @config[:type]
17
+ event[@config[:app].freeze] = @app if @config[:app]
18
+ event[@config[:timestamp].freeze] = current_time(time).iso8601 if @config[:timestamp]
19
19
  "#{@ext.merge(event).to_json}\n"
20
20
  end
21
21
 
@@ -1,3 +1,3 @@
1
1
  module LogFormatter
2
- VERSION = "0.2.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chad_lwm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-25 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler