lumberjack 1.2.2 → 1.2.7

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: 12dffa9034387deb6cfc49976ce1bf2c90b35e9a255ac2f4c1a4730cd3de2a24
4
- data.tar.gz: 1c7d4f246228a9b1d0f35e1618afc245e202d496617590d3d876e1b99b67e6c2
3
+ metadata.gz: 0ba28430c423311445a80559e0bd6fdfbfdfed90edcd4dda89d9e5d635d75ec9
4
+ data.tar.gz: 5507d016ad2b7dcf13e38616496e2b9e5c18e456eb1ad6bdfeceb66386eae403
5
5
  SHA512:
6
- metadata.gz: 24d8eb348cfea7edeba9469e8d7d4c77717aa0b1045050dbce5ecc52f4e53e4c510bb1f8ecf23ea1e3886a7b8bbf0f21870fa1d2f0e964ec4e3dbb0d8af2f8f5
7
- data.tar.gz: cbae4aede2f52a4abf45d3a92729aedbba25a1ef9c58020ab1d120f83bea610e1b83ee10cdc0b35a3ad698685cad6b92925cdaf9009ae1b47cc80f3985e9c4e8
6
+ metadata.gz: 7bf2c9640012487f994d8e4eaabf85e9b4a3fbae828d0a0bccab71cfa7af8820da199b72d04764db4b6d9e7439ac620f49578b0a20b8c77c01f4ea496616ff4c
7
+ data.tar.gz: fb7d9b44ca1bbbd1a2727252c5b9f8885f8a374cd24e0fa34f38b9fca2cfa0444d603d15ccdec743c247251c0622d9153ecbe1b0d9c609da24b38511957ba190
@@ -1,3 +1,25 @@
1
+ ## 1.2.7
2
+
3
+ * Allow passing frozen hashes to `Logger.tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash.
4
+
5
+ ## 1.2.6
6
+
7
+ * Fix Logger#tag so it only ads to the current block's logger tags instead of the global tags if called inside a `Logger#tag` block.
8
+ * Add Logger#remove_tag
9
+
10
+ ## 1.2.5
11
+
12
+ * Fix logic with recursive reference guard in StructuredFormatter so it only suppresses Enumerable references.
13
+ * Add support for bang methods (error!) for setting the log level.
14
+
15
+ ## 1.2.4
16
+
17
+ * Enhance ActiveSupport::TaggedLogging support so code that Lumberjack loggers can be wrapped with a tagged logger.
18
+
19
+ ## 1.2.3
20
+
21
+ * Fix structured formatter so no-recursive, duplicate references are allowed.
22
+
1
23
  ## 1.2.2
2
24
 
3
25
  * Prevent infinite loops in the structured formatter where objects have backreferences to each other.
File without changes
data/README.md CHANGED
@@ -59,8 +59,10 @@ You can specify tags that will only be applied to the logger in a block as well.
59
59
  ```ruby
60
60
  logger.tag(thread_id: Thread.current.object_id) do
61
61
  logger.info("here") # Will include the `thread_id` tag
62
+ logger.tag(count: 15)
63
+ logger.info("with count") # Will include the `count` tag
62
64
  end
63
- logger.info("there") # Will not include the `thread_id` tag
65
+ logger.info("there") # Will not include the `thread_id` or `count` tag
64
66
  ```
65
67
 
66
68
  You can also set tags to `Proc` objects that will be evaluated when creating a log entry.
@@ -85,13 +87,13 @@ logger.info("no requests") # Will not include the `request_id` tag
85
87
 
86
88
  Tag keys are always converted to strings. Tags are inherited so that message tags take precedence over block tags which take precedence over global tags.
87
89
 
88
- #### Compatibility with ActiveSupport::TaggedLogger
90
+ #### Compatibility with ActiveSupport::TaggedLogging
89
91
 
90
- `Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::TaggedLogger`. This is so that other code that expect to have a logger that responds to the `tagged` method will work. Any tags added with the `tagged` method will be appended to an array in the the "tagged" tag. However, if a tagged value has a colon or equals sign in it, it will be parsed to a name value pair.
92
+ `Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::TaggedLogging`. This is so that other code that expect to have a logger that responds to the `tagged` method will work. Any tags added with the `tagged` method will be appended to an array in the the "tagged" tag.
91
93
 
92
94
  ```ruby
93
- logger.tagged("foo", "bar=1", "baz:2", "other") do
94
- logger.info("here") # will include tags: {"tagged" => ["foo", "other"], "bar" => "1", "baz" => "2"}
95
+ logger.tagged("foo", "bar=1", "other") do
96
+ logger.info("here") # will include tags: {"tagged" => ["foo", "bar=1", "other"]}
95
97
  end
96
98
  ```
97
99
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.2.7
@@ -19,6 +19,7 @@ module Lumberjack
19
19
  require_relative "lumberjack/tags.rb"
20
20
  require_relative "lumberjack/tag_formatter.rb"
21
21
  require_relative "lumberjack/tagged_logger_support.rb"
22
+ require_relative "lumberjack/tagged_logging.rb"
22
23
  require_relative "lumberjack/template.rb"
23
24
  require_relative "lumberjack/rack.rb"
24
25
 
@@ -66,6 +67,11 @@ module Lumberjack
66
67
  current_context || Context.new
67
68
  end
68
69
  end
70
+
71
+ # Return true if inside a context block.
72
+ def context?
73
+ !!Thread.current[:lumberjack_context]
74
+ end
69
75
 
70
76
  # Return the tags from the current context or nil if there are no tags.
71
77
  def context_tags
@@ -6,6 +6,9 @@ module Lumberjack
6
6
  class Formatter
7
7
  # Dereference arrays and hashes and recursively call formatters on each element.
8
8
  class StructuredFormatter
9
+ class RecusiveReferenceError < StandardError
10
+ end
11
+
9
12
  def initialize(formatter = nil)
10
13
  @formatter = formatter
11
14
  end
@@ -17,29 +20,44 @@ module Lumberjack
17
20
  private
18
21
 
19
22
  def call_with_references(obj, references)
20
- references << obj
21
23
  if obj.is_a?(Hash)
22
- hash = {}
23
- obj.each do |name, value|
24
- next if references.include?(value)
25
- references << value
26
- hash[name.to_s] = call_with_references(value, references)
24
+ with_object_reference(obj, references) do
25
+ hash = {}
26
+ obj.each do |name, value|
27
+ value = call_with_references(value, references)
28
+ hash[name.to_s] = value unless value.is_a?(RecusiveReferenceError)
29
+ end
30
+ hash
27
31
  end
28
- hash
29
32
  elsif obj.is_a?(Enumerable) && obj.respond_to?(:size) && obj.size != Float::INFINITY
30
- array = []
31
- obj.each do |value|
32
- next if references.include?(value)
33
- references << value
34
- array << call_with_references(value, references)
33
+ with_object_reference(obj, references) do
34
+ array = []
35
+ obj.each do |value|
36
+ value = call_with_references(value, references)
37
+ array << value unless value.is_a?(RecusiveReferenceError)
38
+ end
39
+ array
35
40
  end
36
- array
37
41
  elsif @formatter
38
42
  @formatter.format(obj)
39
43
  else
40
44
  obj
41
45
  end
42
46
  end
47
+
48
+ def with_object_reference(obj, references)
49
+ if obj.is_a?(Enumerable)
50
+ return RecusiveReferenceError.new if references.include?(obj.object_id)
51
+ references << obj.object_id
52
+ begin
53
+ yield
54
+ ensure
55
+ references.delete(obj.object_id)
56
+ end
57
+ else
58
+ yield
59
+ end
60
+ end
43
61
  end
44
62
  end
45
63
  end
@@ -156,10 +156,10 @@ module Lumberjack
156
156
  # logger.add_entry(:warn, "Request took a long time")
157
157
  # logger.add_entry(Logger::DEBUG){"Start processing with options #{options.inspect}"}
158
158
  def add_entry(severity, message, progname = nil, tags = nil)
159
- begin
159
+ begin
160
160
  severity = Severity.label_to_level(severity) unless severity.is_a?(Integer)
161
161
  return true unless device && severity && severity >= level
162
-
162
+
163
163
  return true if Thread.current[:lumberjack_logging]
164
164
  Thread.current[:lumberjack_logging] = true
165
165
 
@@ -238,6 +238,11 @@ module Lumberjack
238
238
  level <= FATAL
239
239
  end
240
240
 
241
+ # Set the log level to fatal.
242
+ def fatal!
243
+ self.level = FATAL
244
+ end
245
+
241
246
  # Log an +ERROR+ message. The message can be passed in either the +message+ argument or in a block.
242
247
  def error(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
243
248
  call_add_entry(ERROR, message_or_progname_or_tags, progname_or_tags, &block)
@@ -248,6 +253,11 @@ module Lumberjack
248
253
  level <= ERROR
249
254
  end
250
255
 
256
+ # Set the log level to error.
257
+ def error!
258
+ self.level = ERROR
259
+ end
260
+
251
261
  # Log a +WARN+ message. The message can be passed in either the +message+ argument or in a block.
252
262
  def warn(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
253
263
  call_add_entry(WARN, message_or_progname_or_tags, progname_or_tags, &block)
@@ -258,6 +268,11 @@ module Lumberjack
258
268
  level <= WARN
259
269
  end
260
270
 
271
+ # Set the log level to warn.
272
+ def warn!
273
+ self.level = WARN
274
+ end
275
+
261
276
  # Log an +INFO+ message. The message can be passed in either the +message+ argument or in a block.
262
277
  def info(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
263
278
  call_add_entry(INFO, message_or_progname_or_tags, progname_or_tags, &block)
@@ -268,6 +283,11 @@ module Lumberjack
268
283
  level <= INFO
269
284
  end
270
285
 
286
+ # Set the log level to info.
287
+ def info!
288
+ self.level = INFO
289
+ end
290
+
271
291
  # Log a +DEBUG+ message. The message can be passed in either the +message+ argument or in a block.
272
292
  def debug(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
273
293
  call_add_entry(DEBUG, message_or_progname_or_tags, progname_or_tags, &block)
@@ -278,6 +298,11 @@ module Lumberjack
278
298
  level <= DEBUG
279
299
  end
280
300
 
301
+ # Set the log level to debug.
302
+ def debug!
303
+ self.level = DEBUG
304
+ end
305
+
281
306
  # Log a message when the severity is not known. Unknown messages will always appear in the log.
282
307
  # The message can be passed in either the +message+ argument or in a block.
283
308
  def unknown(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
@@ -324,16 +349,34 @@ module Lumberjack
324
349
  end
325
350
 
326
351
  # Set a hash of tags on logger. If a block is given, the tags will only be set
327
- # for the duration of the block.
352
+ # for the duration of the block. If this method is called inside such a block,
353
+ # the tags will only be defined on the tags in that block. When the parent block
354
+ # exits, all the tags will be reverted. If there is no block, then the tags will
355
+ # be defined as global and apply to all log statements.
328
356
  def tag(tags, &block)
329
357
  tags = Tags.stringify_keys(tags)
358
+ thread_tags = thread_local_value(:lumberjack_logger_tags)
330
359
  if block
331
- thread_tags = thread_local_value(:lumberjack_logger_tags)
332
- value = (thread_tags ? thread_tags.merge(tags) : tags)
333
- push_thread_local_value(:lumberjack_logger_tags, value, &block)
360
+ merged_tags = (thread_tags ? thread_tags.merge(tags) : tags.dup)
361
+ push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block)
362
+ elsif thread_tags
363
+ thread_tags.merge!(tags)
334
364
  else
335
365
  @tags.merge!(tags)
336
366
  end
367
+ nil
368
+ end
369
+
370
+ # Remove a tag from the current tag context. If this is called inside a block to a
371
+ # call to `tag`, the tags will only be removed for the duration of that block. Otherwise
372
+ # they will be removed from the global tags.
373
+ def remove_tag(*tag_names)
374
+ thread_tags = thread_local_value(:lumberjack_logger_tags)
375
+ if thread_tags
376
+ tag_names.each { |name| thread_tags.delete(name.to_s) }
377
+ else
378
+ tag_names.each { |name| @tags.delete(name.to_s) }
379
+ end
337
380
  end
338
381
 
339
382
  # Return all tags in scope on the logger including global tags set on the Lumberjack
@@ -449,7 +492,7 @@ module Lumberjack
449
492
  sleep(flush_seconds)
450
493
  logger.flush if Time.now - logger.last_flushed_at >= flush_seconds
451
494
  rescue => e
452
- STDERR.puts("Error flushing log: #{e.inspect}")
495
+ $stderr.puts("Error flushing log: #{e.inspect}")
453
496
  end
454
497
  end
455
498
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lumberjack
4
+ # Monkey patch for ActiveSupport::TaggedLogger so it doesn't blow up when
5
+ # a Lumberjack logger is trying to be wrapped. This module will be automatically
6
+ # included in ActiveSupport::TaggedLogger if activesupport is already loaded.
7
+ module TaggedLogging
8
+ class << self
9
+ def included(base)
10
+ base.singleton_class.send(:prepend, ClassMethods)
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def new(logger)
16
+ if logger.is_a?(Lumberjack::Logger)
17
+ logger = logger.tagged_logger! unless logger.respond_to?(:tagged)
18
+ logger
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ if defined?(ActiveSupport::TaggedLogging)
28
+ ActiveSupport::TaggedLogging.include(Lumberjack::TaggedLogging)
29
+ end
@@ -11,8 +11,7 @@ Gem::Specification.new do |spec|
11
11
  # Specify which files should be added to the gem when it is released.
12
12
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
13
13
  ignore_files = %w(
14
- .gitignore
15
- .travis.yml
14
+ .
16
15
  Appraisals
17
16
  Gemfile
18
17
  Gemfile.lock
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lumberjack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-05 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -74,7 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - CHANGELOG.md
77
- - MIT_LICENSE
77
+ - MIT_LICENSE.txt
78
78
  - README.md
79
79
  - VERSION
80
80
  - lib/lumberjack.rb
@@ -106,6 +106,7 @@ files:
106
106
  - lib/lumberjack/severity.rb
107
107
  - lib/lumberjack/tag_formatter.rb
108
108
  - lib/lumberjack/tagged_logger_support.rb
109
+ - lib/lumberjack/tagged_logging.rb
109
110
  - lib/lumberjack/tags.rb
110
111
  - lib/lumberjack/template.rb
111
112
  - lumberjack.gemspec