mimi-logger 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: 1dac6f3cbaeef0582579cb7f695e92896676d9267c286e7d82d95d7d72312219
4
- data.tar.gz: 3e9866931dff24f400dca04da3e1e41d59139f33860c621d2e7136656f80f87d
3
+ metadata.gz: fc1a61b8b50fe78090ba13f5e6c84f492e2477d3b787bf309e31e45b53bcc552
4
+ data.tar.gz: 4135fb3a61883a0bfb507a5fd753497bc2fb251f7c30f35ca2beff912b6a63d9
5
5
  SHA512:
6
- metadata.gz: 0c02fa6f4e89c807b5b6284aec1e304939cf3aec08820c73db08380b81a0b0f071e85e99c55810bf8e0b584a5497f227748311dd44b7d4e9b087b649970a2b2a
7
- data.tar.gz: 62cdc14d5fcbfe555e2fe45af17db5c004d051767445802dbb35fa3525825f4de514afad51da9fd700f110ae4c1ad70680344da47641b3bba50250a7c2dfdf9e
6
+ metadata.gz: 3ba848ac8604e7b000eb303996f1cc73abd9e7652edfed6c65f2e38672849d30b3af99215ffac0f1d44ef422da8f7f4bc5694a53d57f570c45921c3bb616a719
7
+ data.tar.gz: 90ab9b7c5e6587e704059d9b0d59630fe70de3fdacd8269d5e644a1ab35a7466f8016bfb14c981975f5f663db7e1392e4a745ad51fa3173e7627809afd67cf8b
data/README.md CHANGED
@@ -63,6 +63,50 @@ logger.info 'Jobs done', t: Time.now - t_start
63
63
  # => '{"s":"I","m":"Jobs done","c":"60eecc2e764fe2f6", "t": 13.794034499}'
64
64
  ```
65
65
 
66
+ ### How to log structured data
67
+
68
+ There are multiple ways to log an event. The first and the simplest one is to log just a text
69
+ message:
70
+
71
+ ```ruby
72
+ require 'mimi/logger'
73
+
74
+ logger = Mimi::Logger.new
75
+
76
+ logger.info 'I am a banana'
77
+ # or using a block variant
78
+ logger.debug { 'Debug this banana' }
79
+ ```
80
+
81
+ Alternatively you can log a structured object, by passing a Hash in addition to the message:
82
+
83
+ ```ruby
84
+ require 'mimi/logger'
85
+
86
+ logger = Mimi::Logger.new
87
+
88
+ logger.info 'I am a banana', banana: { id: 123, weight: 456 }
89
+ # => {"s":"I","m":"I am a banana","c":"d8b6f859bf9d0190","banana":{"id":123,"weight":456}}
90
+ ```
91
+
92
+ Or specify the object/Hash explicitly:
93
+ ```ruby
94
+ ...
95
+ logger.info m: 'I am a banana', banana: { id: 123, weight: 456 }
96
+ ```
97
+
98
+ Or with a block variant:
99
+ ```ruby
100
+ ...
101
+ logger.debug do
102
+ { banana: { id: 123, weight: 456 } }
103
+ end
104
+
105
+ # a block can also return an Array of one (Hash) or two (String,Hash) elements:
106
+ logger.debug { [m: 'Debug this banana', banana: { id: 123, weight: 456 }] }
107
+ logger.debug { ['Debug this banana', banana: { id: 123, weight: 456 }] }
108
+ ```
109
+
66
110
  ### Logging context
67
111
 
68
112
  Logging context refers to a set of instructions that are somehow logically grouped. For example,
@@ -16,7 +16,7 @@ module Mimi
16
16
  extend Forwardable
17
17
  include Mimi::Core::Module
18
18
 
19
- attr_reader :logger_instance
19
+ attr_reader :logger_instance, :options
20
20
  delegate [
21
21
  :debug?, :info?, :warn?, :error?, :fatal?,
22
22
  :<<, :add, :log
@@ -49,11 +49,11 @@ module Mimi
49
49
  opts ||= {}
50
50
  raise ArgumentError, '(io, opts) are expected as parameters' unless args.empty?
51
51
 
52
- opts = self.class.module_options.deep_merge(opts)
52
+ @options = self.class.module_options.deep_merge(opts)
53
53
  @logger_instance = ::Logger.new(io)
54
- @logger_instance.level = self.class.level_from_any(opts[:level])
54
+ @logger_instance.level = self.class.level_from_any(options[:level])
55
55
  io.sync = true if io.respond_to?(:sync=)
56
- @logger_instance.formatter = self.class.formatter
56
+ @logger_instance.formatter = self.class.formatter(options)
57
57
  end
58
58
 
59
59
  # Returns the current log level
@@ -124,12 +124,12 @@ module Mimi
124
124
  #
125
125
  # @return [Proc]
126
126
  #
127
- def self.formatter
128
- case module_options[:format].to_s
127
+ def self.formatter(options)
128
+ case options[:format].to_s
129
129
  when 'json'
130
- formatter_json
130
+ formatter_json(options)
131
131
  when 'string'
132
- formatter_string
132
+ formatter_string(options)
133
133
  else
134
134
  raise "Invalid format specified for Mimi::Logger: '#{module_options[:format]}'"
135
135
  end
@@ -137,12 +137,14 @@ module Mimi
137
137
 
138
138
  # Returns formatter for 'json' format
139
139
  #
140
+ # @param options [Hash] logger options
141
+ #
140
142
  # @return [Proc]
141
143
  #
142
- def self.formatter_json
144
+ def self.formatter_json(options)
143
145
  proc do |severity, _datetime, _progname, message|
144
146
  h = formatter_message_args_to_hash(message)
145
- h[:c] = context_id if module_options[:log_context]
147
+ h[:c] = context_id if options[:log_context]
146
148
  h[:s] = severity.to_s[0]
147
149
  h.to_json
148
150
  end
@@ -150,15 +152,17 @@ module Mimi
150
152
 
151
153
  # Returns formatter for 'string' format
152
154
  #
155
+ # @param options [Hash] logger options
156
+ #
153
157
  # @return [Proc]
154
158
  #
155
- def self.formatter_string
159
+ def self.formatter_string(options)
156
160
  proc do |severity, _datetime, _progname, message|
157
161
  h = formatter_message_args_to_hash(message)
158
162
  parts = []
159
163
  parts << severity.to_s[0]
160
- parts << context_id if module_options[:log_context]
161
- parts << h[:m].to_s.tr("\n", module_options[:cr_character])
164
+ parts << context_id if options[:log_context]
165
+ parts << h[:m].to_s.tr("\n", options[:cr_character])
162
166
  parts << '...' unless h.except(:m).empty?
163
167
  parts.join(', ')
164
168
  end
@@ -1,5 +1,5 @@
1
1
  module Mimi
2
2
  class Logger
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mimi-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-18 00:00:00.000000000 Z
11
+ date: 2018-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimi-core