mimi-logger 0.2.2 → 0.2.3

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: 7d79dac353758f9f0364896918dfb9929f3ba972a727eb93830b32ae209eaa4e
4
- data.tar.gz: 4e0400b25ce0af579740fa2fd567ea2ba60e1d36fb5b5189a301049db3e3d8e0
3
+ metadata.gz: '08802d5b0a249e93a152ad6e55b97dac5f90235815621cf4e184f98f4579f8b0'
4
+ data.tar.gz: 0ed0db974edfae6ac95a1cbd4d3ad44328c1e2c77013598dc41811b5c83d0dba
5
5
  SHA512:
6
- metadata.gz: 5c7d372b6eb408b2fbf5cde056708e4195de48a251e540ae5fd31edb6e525afacbda22b9ec9003d89ed9f6f699c092ae2af4a7e5397fae237e0e13d3081232ce
7
- data.tar.gz: 9c4db7213fb0f11925bb6a8430e8dcc12c15fbe17c888717ec385be70a60b0a808c48372684463eaa1241908c930ca591e9cb349aadf1c903cb168b95c11f523
6
+ metadata.gz: fd0d2e2ff59c4389a4cc0bbde0e594d1bb73a7931df7137b9e31126b42f884ed473747473fe18540721dd93fd670ffed38c6cfe299a97052081eec5a68fe0322
7
+ data.tar.gz: a6ba83b28d31f0586dd1c151fb944315d0d192abf44799c50206544e0ae869897baa5c4f9d392ee6861b57fb22668af4745bbfd9a5d2bc505308f743898ddf92
data/README.md CHANGED
@@ -118,7 +118,7 @@ web requests will be interleaved and understanding the flow, the cause and effec
118
118
  To solve this problem, Mimi::Logger allows for setting an arbitrary (or random) context ID, which is local
119
119
  to the current thread, and which is included in every logged message.
120
120
 
121
- A new context may be initiated by `.new_context_id!`:
121
+ A new context may be initiated by `.new_context!`:
122
122
 
123
123
  ```ruby
124
124
  require 'mimi/logger'
@@ -126,7 +126,7 @@ require 'mimi/logger'
126
126
  logger = Mimi::Logger.new
127
127
 
128
128
  logger.info 'I am a banana!'
129
- logger.new_context_id!
129
+ logger.new_context!
130
130
  logger.info 'I am a banana!' # this is not the same banana, it's from a different context
131
131
  ```
132
132
 
@@ -156,6 +156,40 @@ new context ID in the beginning of your processing and pass it along with all th
156
156
  other components of the system. Upon receiving such a request, another application sets its local context ID
157
157
  to the received value and continues.
158
158
 
159
+ #### Preserving context
160
+
161
+ If necessary, it is possible to run a block of code in another logging context and restore
162
+ the original context after completion:
163
+
164
+ ```ruby
165
+ require 'mimi/logger'
166
+
167
+ logger = Mimi::Logger.new
168
+
169
+ logger.context_id = 'original-context'
170
+ logger.with_preserved_context do
171
+ logger.context_id = 'another-context'
172
+ ...
173
+ end
174
+ logger.context_id # => "original-context"
175
+ ```
176
+
177
+ Or run a block of code with a new temporary context and restore the original one after completion:
178
+
179
+ ```ruby
180
+ require 'mimi/logger'
181
+
182
+ logger = Mimi::Logger.new
183
+
184
+ logger.context_id = 'original-context'
185
+ logger.with_new_context do
186
+ logger.context_id # => "5d11f7c483dcfb2a"
187
+ ...
188
+ end
189
+ logger.context_id # => "original-context"
190
+ ```
191
+
192
+
159
193
  ## Contributing
160
194
 
161
195
  Bug reports and pull requests are welcome on GitHub at https://github.com/kukushkin/mimi-logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -1,5 +1,5 @@
1
1
  module Mimi
2
2
  class Logger
3
- VERSION = '0.2.2'.freeze
3
+ VERSION = '0.2.3'.freeze
4
4
  end
5
5
  end
data/lib/mimi/logger.rb CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require 'forwardable'
@@ -17,10 +18,9 @@ module Mimi
17
18
  include Mimi::Core::Module
18
19
 
19
20
  attr_reader :logger_instance, :options
20
- delegate [
21
- :debug?, :info?, :warn?, :error?, :fatal?,
22
- :<<, :add, :log
23
- ] => :logger_instance
21
+
22
+ delegate %i[debug? info? warn? error? fatal? << add log] => :logger_instance
23
+ delegate %i[context_id context_id= new_context! with_new_context with_preserved_context] => self
24
24
 
25
25
  default_options(
26
26
  format: 'json', # 'string' or 'json'
@@ -75,23 +75,36 @@ module Mimi
75
75
 
76
76
  # Logs a new message at the corresponding logging level
77
77
  #
78
- #
79
- #
80
78
  def debug(*args, &block)
81
79
  logger_instance.debug(args, &block)
82
80
  end
81
+
82
+ # Logs a new message at the corresponding logging level
83
+ #
83
84
  def info(*args, &block)
84
85
  logger_instance.info(args, &block)
85
86
  end
87
+
88
+ # Logs a new message at the corresponding logging level
89
+ #
86
90
  def warn(*args, &block)
87
91
  logger_instance.warn(args, &block)
88
92
  end
93
+
94
+ # Logs a new message at the corresponding logging level
95
+ #
89
96
  def error(*args, &block)
90
97
  logger_instance.error(args, &block)
91
98
  end
99
+
100
+ # Logs a new message at the corresponding logging level
101
+ #
92
102
  def fatal(*args, &block)
93
103
  logger_instance.fatal(args, &block)
94
104
  end
105
+
106
+ # Logs a new message at the corresponding logging level
107
+ #
95
108
  def unknown(*args, &block)
96
109
  logger_instance.unknown(args, &block)
97
110
  end
@@ -123,6 +136,7 @@ module Mimi
123
136
  # Returns formatter Proc object depending on configured format
124
137
  #
125
138
  # @return [Proc]
139
+ # @private
126
140
  #
127
141
  def self.formatter(options)
128
142
  case options[:format].to_s
@@ -138,8 +152,8 @@ module Mimi
138
152
  # Returns formatter for 'json' format
139
153
  #
140
154
  # @param options [Hash] logger options
141
- #
142
155
  # @return [Proc]
156
+ # @private
143
157
  #
144
158
  def self.formatter_json(options)
145
159
  proc do |severity, _datetime, _progname, message|
@@ -153,8 +167,8 @@ module Mimi
153
167
  # Returns formatter for 'string' format
154
168
  #
155
169
  # @param options [Hash] logger options
156
- #
157
170
  # @return [Proc]
171
+ # @private
158
172
  #
159
173
  def self.formatter_string(options)
160
174
  proc do |severity, _datetime, _progname, message|
@@ -183,6 +197,7 @@ module Mimi
183
197
  # message and the rest are optional parameters passed in a Hash argument.
184
198
  #
185
199
  # @return [Hash]
200
+ # @private
186
201
  #
187
202
  def self.formatter_message_args_to_hash(message_args)
188
203
  message_args = message_args.is_a?(String) || message_args.is_a?(Hash) ? [message_args] : message_args
@@ -218,44 +233,65 @@ module Mimi
218
233
  # happening within same logical context, as a single operation. For example, processing
219
234
  # an incoming request may be seen as a single context.
220
235
  #
221
- # Context ID is logged with every message
236
+ # Context ID is logged with every message.
222
237
  #
223
238
  # @return [String] a hex encoded context ID
224
239
  #
225
240
  def self.context_id
226
- Thread.current[CONTEXT_ID_THREAD_VARIABLE] || new_context_id!
227
- end
228
-
229
- def context_id
230
- self.class.context_id
241
+ Thread.current[CONTEXT_ID_THREAD_VARIABLE] || new_context!
231
242
  end
232
243
 
233
244
  # Sets the new context ID to the given value
234
245
  #
235
- # @param id [String]
246
+ # @param id [String] a new context ID
247
+ # @return [String]
236
248
  #
237
249
  def self.context_id=(id)
238
250
  Thread.current[CONTEXT_ID_THREAD_VARIABLE] = id
239
251
  end
240
252
 
241
- def context_id=(id)
242
- self.class.context_id = id
243
- end
244
-
245
- # Generates a new random context ID and sets it as current
253
+ # Starts a new logging context, generates a new random context ID and sets it as current
246
254
  #
247
255
  # @return [String] a new context ID
248
256
  #
249
- def self.new_context_id!
257
+ def self.new_context!
250
258
  self.context_id = SecureRandom.hex(CONTEXT_ID_SIZE)
251
259
  end
252
260
 
253
- def new_context_id!
254
- self.class.new_context_id!
261
+ # Executes a given block and ensures the context is restored afterwards
262
+ #
263
+ # @example
264
+ # logger.context_id # => "5d11f7c483dcfb2a"
265
+ # logger.with_preserved_context do
266
+ # logger.context_id = "temporary-context"
267
+ # logger.context_id # => "temporary-context"
268
+ # end
269
+ # logger.context_id # => "5d11f7c483dcfb2a"
270
+ #
271
+ def self.with_preserved_context(&_block)
272
+ preserved_context_id = context_id
273
+ yield
274
+ ensure
275
+ self.context_id = preserved_context_id
255
276
  end
256
277
 
257
- # private-ish
278
+ # Executes a given block in a new context and restores the context afterwards
279
+ #
280
+ # @example
281
+ # logger.context_id # => "5d11f7c483dcfb2a"
282
+ # logger.with_new_context do
283
+ # logger.context_id # => "e211ef95633a04b5"
284
+ # end
285
+ # logger.context_id # => "5d11f7c483dcfb2a"
286
+ #
287
+ def self.with_new_context(&_block)
288
+ with_preserved_context do
289
+ new_context!
290
+ yield
291
+ end
292
+ end
258
293
 
294
+ # @private
259
295
  def self.module_manifest
260
296
  {
261
297
  log_level: {
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.2
4
+ version: 0.2.3
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-19 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimi-core