lumberjack 1.2.1 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/{MIT_LICENSE → MIT_LICENSE.txt} +0 -0
- data/README.md +7 -5
- data/VERSION +1 -1
- data/lib/lumberjack.rb +6 -0
- data/lib/lumberjack/formatter/structured_formatter.rb +38 -6
- data/lib/lumberjack/logger.rb +50 -7
- data/lib/lumberjack/tagged_logging.rb +29 -0
- data/lumberjack.gemspec +1 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb579edefef20d72b221b245b502657e3e7b60b61fd2037e6d97ff50577b0d6d
|
4
|
+
data.tar.gz: a38ac097040fd058346f2329d2b014d98e465e1f842ca466e52ede4bd5303463
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 196113e296216a6cf5e5b5914ecc041b9bdb2be71d0847d1bf7bc15cd14aca05a8e2d62f6c2ff397a5323b18a67e714aa19bf3f3eb837c88753254dc5d4226ee
|
7
|
+
data.tar.gz: d4ee3d9b4d90464517ce7b8546304b8dfdcb4e709e839d9d828af2b409199bea5d9943860b238156a4c291e1d7cdd12a502dfe365350bce001a6d231d53bd058
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
## 1.2.6
|
2
|
+
|
3
|
+
* 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.
|
4
|
+
* Add Logger#remove_tag
|
5
|
+
|
6
|
+
## 1.2.5
|
7
|
+
|
8
|
+
* Fix logic with recursive reference guard in StructuredFormatter so it only suppresses Enumerable references.
|
9
|
+
* Add support for bang methods (error!) for setting the log level.
|
10
|
+
|
11
|
+
## 1.2.4
|
12
|
+
|
13
|
+
* Enhance ActiveSupport::TaggedLogging support so code that Lumberjack loggers can be wrapped with a tagged logger.
|
14
|
+
|
15
|
+
## 1.2.3
|
16
|
+
|
17
|
+
* Fix structured formatter so no-recursive, duplicate references are allowed.
|
18
|
+
|
19
|
+
## 1.2.2
|
20
|
+
|
21
|
+
* Prevent infinite loops in the structured formatter where objects have backreferences to each other.
|
22
|
+
|
1
23
|
## 1.2.1
|
2
24
|
|
3
25
|
* Prevent infinite loops where logging a statement triggers the logger.
|
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::
|
90
|
+
#### Compatibility with ActiveSupport::TaggedLogging
|
89
91
|
|
90
|
-
`Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::
|
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", "
|
94
|
-
logger.info("here") # will include tags: {"tagged" => ["foo", "
|
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.
|
1
|
+
1.2.6
|
data/lib/lumberjack.rb
CHANGED
@@ -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,26 +6,58 @@ 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
|
12
15
|
|
13
16
|
def call(obj)
|
17
|
+
call_with_references(obj, Set.new)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def call_with_references(obj, references)
|
14
23
|
if obj.is_a?(Hash)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
19
31
|
end
|
20
|
-
hash
|
21
32
|
elsif obj.is_a?(Enumerable) && obj.respond_to?(:size) && obj.size != Float::INFINITY
|
22
|
-
obj
|
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
|
40
|
+
end
|
23
41
|
elsif @formatter
|
24
42
|
@formatter.format(obj)
|
25
43
|
else
|
26
44
|
obj
|
27
45
|
end
|
28
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
|
29
61
|
end
|
30
62
|
end
|
31
63
|
end
|
data/lib/lumberjack/logger.rb
CHANGED
@@ -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
|
-
|
332
|
-
|
333
|
-
|
360
|
+
merged_tags = (thread_tags ? thread_tags.merge(tags) : tags)
|
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
|
-
|
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
|
data/lumberjack.gemspec
CHANGED
@@ -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
|
-
.
|
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.
|
4
|
+
version: 1.2.6
|
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-
|
11
|
+
date: 2020-06-20 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
|