contextual_logger 0.12.0.pre.1 → 1.1.0
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/lib/contextual_logger/context/handler.rb +2 -4
- data/lib/contextual_logger/redactor.rb +5 -1
- data/lib/contextual_logger/version.rb +1 -1
- data/lib/contextual_logger.rb +6 -24
- metadata +5 -11
- data/lib/contextual_logger/context/registry.rb +0 -73
- data/lib/contextual_logger/context/registry_types/boolean.rb +0 -28
- data/lib/contextual_logger/context/registry_types/date.rb +0 -28
- data/lib/contextual_logger/context/registry_types/hash.rb +0 -78
- data/lib/contextual_logger/context/registry_types/number.rb +0 -28
- data/lib/contextual_logger/context/registry_types/string.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60008f094eadb7217e227f18e231ccf5860747d3a2ee59fd3d72cf2fd07e657d
|
4
|
+
data.tar.gz: 55f4cb698205f61e6e3b75ff1f920a9f85cab16ee3078ced7f18c2f877588010
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a81f1ff4730f739c4a84cfe98c88dfca8dcbfb770418dc8a7ad0a9d24089126556cffd0d381f773c732116feea19545476904e255fbff304f710eefb4ab9938
|
7
|
+
data.tar.gz: 51b7f598a1fa3d4d84b488134f4cbc7eb51ab585d79995c9e08b9e93f72bb8ef0043fb9aefd31166b89b111fa22c85b111ea92dd6b8099841759588dd266ffa0
|
@@ -7,10 +7,8 @@ module ContextualLogger
|
|
7
7
|
|
8
8
|
attr_reader :previous_context, :context
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
|
13
|
-
end
|
10
|
+
def self.current_context
|
11
|
+
Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
|
14
12
|
end
|
15
13
|
|
16
14
|
def initialize(context, previous_context: nil)
|
@@ -10,7 +10,11 @@ module ContextualLogger
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def register_secret(sensitive_data)
|
13
|
-
|
13
|
+
register_regex(Regexp.escape(sensitive_data))
|
14
|
+
end
|
15
|
+
|
16
|
+
def register_regex(regex)
|
17
|
+
if redaction_set.add?(regex)
|
14
18
|
@redaction_regex = Regexp.new(
|
15
19
|
redaction_set.to_a.join('|')
|
16
20
|
)
|
data/lib/contextual_logger.rb
CHANGED
@@ -5,7 +5,6 @@ require 'active_support/core_ext/module/delegation'
|
|
5
5
|
require 'json'
|
6
6
|
require_relative './contextual_logger/redactor'
|
7
7
|
require_relative './contextual_logger/context/handler'
|
8
|
-
require_relative './contextual_logger/context/registry'
|
9
8
|
|
10
9
|
module ContextualLogger
|
11
10
|
LOG_LEVEL_NAMES_TO_SEVERITY =
|
@@ -46,22 +45,12 @@ module ContextualLogger
|
|
46
45
|
module LoggerMixin
|
47
46
|
delegate :register_secret, to: :redactor
|
48
47
|
|
49
|
-
def register_context(&block)
|
50
|
-
block or raise ArgumentError, 'Block of context definitions was not passed'
|
51
|
-
@context_registry = Context::Registry.new(&block)
|
52
|
-
end
|
53
|
-
|
54
48
|
def global_context=(context)
|
55
|
-
Context::Handler.new(
|
49
|
+
Context::Handler.new(context).set!
|
56
50
|
end
|
57
51
|
|
58
52
|
def with_context(context)
|
59
|
-
context_handler = Context::Handler.new(
|
60
|
-
current_context_for_thread.deep_merge(
|
61
|
-
context_registry.format(context)
|
62
|
-
)
|
63
|
-
)
|
64
|
-
|
53
|
+
context_handler = Context::Handler.new(current_context_for_thread.deep_merge(context))
|
65
54
|
context_handler.set!
|
66
55
|
if block_given?
|
67
56
|
begin
|
@@ -96,11 +85,11 @@ module ContextualLogger
|
|
96
85
|
add(#{log_level}, nil, arg, &block)
|
97
86
|
else
|
98
87
|
if arg.nil?
|
99
|
-
add(#{log_level}, nil, context, &block)
|
88
|
+
add(#{log_level}, nil, **context, &block)
|
100
89
|
elsif block
|
101
|
-
add(#{log_level}, nil, context.merge(progname: arg), &block)
|
90
|
+
add(#{log_level}, nil, **context.merge(progname: arg), &block)
|
102
91
|
else
|
103
|
-
add(#{log_level}, arg, context)
|
92
|
+
add(#{log_level}, arg, **context)
|
104
93
|
end
|
105
94
|
end
|
106
95
|
end
|
@@ -128,7 +117,7 @@ module ContextualLogger
|
|
128
117
|
message = arg1
|
129
118
|
progname = arg2 || @progname
|
130
119
|
end
|
131
|
-
write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(
|
120
|
+
write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(context))
|
132
121
|
end
|
133
122
|
|
134
123
|
true
|
@@ -144,13 +133,6 @@ module ContextualLogger
|
|
144
133
|
|
145
134
|
private
|
146
135
|
|
147
|
-
def context_registry
|
148
|
-
@context_registry ||= Context::Registry.new do
|
149
|
-
strict false
|
150
|
-
raise_on_missing_definition false
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
136
|
def redactor
|
155
137
|
@redactor ||= Redactor.new
|
156
138
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contextual_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Ebentier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -46,12 +46,6 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- lib/contextual_logger.rb
|
48
48
|
- lib/contextual_logger/context/handler.rb
|
49
|
-
- lib/contextual_logger/context/registry.rb
|
50
|
-
- lib/contextual_logger/context/registry_types/boolean.rb
|
51
|
-
- lib/contextual_logger/context/registry_types/date.rb
|
52
|
-
- lib/contextual_logger/context/registry_types/hash.rb
|
53
|
-
- lib/contextual_logger/context/registry_types/number.rb
|
54
|
-
- lib/contextual_logger/context/registry_types/string.rb
|
55
49
|
- lib/contextual_logger/logger_with_context.rb
|
56
50
|
- lib/contextual_logger/overrides/active_support/tagged_logging/formatter.rb
|
57
51
|
- lib/contextual_logger/redactor.rb
|
@@ -73,11 +67,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
67
|
version: '0'
|
74
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
69
|
requirements:
|
76
|
-
- - "
|
70
|
+
- - ">="
|
77
71
|
- !ruby/object:Gem::Version
|
78
|
-
version:
|
72
|
+
version: '0'
|
79
73
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
74
|
+
rubygems_version: 3.1.6
|
81
75
|
signing_key:
|
82
76
|
specification_version: 4
|
83
77
|
summary: Add context to your logger
|
@@ -1,73 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'registry_types/string'
|
4
|
-
require_relative 'registry_types/boolean'
|
5
|
-
require_relative 'registry_types/number'
|
6
|
-
require_relative 'registry_types/date'
|
7
|
-
require_relative 'registry_types/hash'
|
8
|
-
|
9
|
-
# This class is responsible for holding the registered context shape that will
|
10
|
-
# be used by the LoggerMixin to make sure that the context matches the shape
|
11
|
-
# defined
|
12
|
-
|
13
|
-
# logger.configure_context do
|
14
|
-
# strict false
|
15
|
-
#
|
16
|
-
# string test_string
|
17
|
-
# integer test_integer
|
18
|
-
# hash :test_hash do
|
19
|
-
# string :test_string_in_hash
|
20
|
-
# date :date_time_in_hash
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
|
24
|
-
module ContextualLogger
|
25
|
-
module Context
|
26
|
-
class Registry < RegistryTypes::Hash
|
27
|
-
class DuplicateDefinitionError < StandardError; end
|
28
|
-
class MissingDefinitionError < StandardError; end
|
29
|
-
|
30
|
-
def initialize(&definitions)
|
31
|
-
@strict = true
|
32
|
-
@raise_on_missing_definition = true
|
33
|
-
|
34
|
-
super
|
35
|
-
end
|
36
|
-
|
37
|
-
def strict?
|
38
|
-
@strict
|
39
|
-
end
|
40
|
-
|
41
|
-
def raise_on_missing_definition?
|
42
|
-
@raise_on_missing_definition
|
43
|
-
end
|
44
|
-
|
45
|
-
def format(context)
|
46
|
-
if strict?
|
47
|
-
super(context, raise_on_missing_definition?)
|
48
|
-
else
|
49
|
-
context
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
alias context_shape to_h
|
54
|
-
|
55
|
-
def to_h
|
56
|
-
{
|
57
|
-
strict: @strict,
|
58
|
-
context_shape: context_shape
|
59
|
-
}
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def strict(value)
|
65
|
-
@strict = value
|
66
|
-
end
|
67
|
-
|
68
|
-
def raise_on_missing_definition(value)
|
69
|
-
@raise_on_missing_definition = value
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ContextualLogger
|
4
|
-
module Context
|
5
|
-
module RegistryTypes
|
6
|
-
class Boolean
|
7
|
-
attr_reader :formatter
|
8
|
-
|
9
|
-
def initialize(formatter: nil)
|
10
|
-
@formatter = formatter || ->(value) { value ? true : false }
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_h
|
14
|
-
{ type: :boolean, formatter: formatter }
|
15
|
-
end
|
16
|
-
|
17
|
-
def format(value)
|
18
|
-
case formatter
|
19
|
-
when Proc
|
20
|
-
formatter.call(value)
|
21
|
-
else
|
22
|
-
value.send(formatter)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ContextualLogger
|
4
|
-
module Context
|
5
|
-
module RegistryTypes
|
6
|
-
class Date
|
7
|
-
attr_reader :formatter
|
8
|
-
|
9
|
-
def initialize(formatter: nil)
|
10
|
-
@formatter = formatter || ->(value) { value.iso8601(6) }
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_h
|
14
|
-
{ type: :date, formatter: formatter }
|
15
|
-
end
|
16
|
-
|
17
|
-
def format(value)
|
18
|
-
case formatter
|
19
|
-
when Proc
|
20
|
-
formatter.call(value)
|
21
|
-
else
|
22
|
-
value.send(formatter)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,78 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'string'
|
4
|
-
require_relative 'boolean'
|
5
|
-
require_relative 'number'
|
6
|
-
require_relative 'date'
|
7
|
-
|
8
|
-
module ContextualLogger
|
9
|
-
module Context
|
10
|
-
module RegistryTypes
|
11
|
-
class Hash
|
12
|
-
def initialize(&definitions)
|
13
|
-
@definitions = {}
|
14
|
-
|
15
|
-
run(&definitions)
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_h
|
19
|
-
@definitions.reduce({}) do |shape_hash, (key, value)|
|
20
|
-
shape_hash.merge(key => value.to_h)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def format(context, raise_on_missing_definition)
|
25
|
-
context.reduce({}) do |formatted_context, (key, value)|
|
26
|
-
if (definition = @definitions[key])
|
27
|
-
formatted_context[key] = if definition.is_a?(RegistryTypes::Hash)
|
28
|
-
definition.format(value, raise_on_missing_definition)
|
29
|
-
else
|
30
|
-
definition.format(value)
|
31
|
-
end
|
32
|
-
elsif raise_on_missing_definition
|
33
|
-
raise Registry::MissingDefinitionError, "Attempting to apply context #{key} that is missing a definition in the registry"
|
34
|
-
end
|
35
|
-
|
36
|
-
formatted_context
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def string(context_key, formatter: nil)
|
43
|
-
dedup(context_key, :string)
|
44
|
-
@definitions[context_key] = RegistryTypes::String.new(formatter: formatter)
|
45
|
-
end
|
46
|
-
|
47
|
-
def boolean(context_key, formatter: nil)
|
48
|
-
dedup(context_key, :boolean)
|
49
|
-
@definitions[context_key] = RegistryTypes::Boolean.new(formatter: formatter)
|
50
|
-
end
|
51
|
-
|
52
|
-
def number(context_key, formatter: nil)
|
53
|
-
dedup(context_key, :number)
|
54
|
-
@definitions[context_key] = RegistryTypes::Number.new(formatter: formatter)
|
55
|
-
end
|
56
|
-
|
57
|
-
def date(context_key, formatter: nil)
|
58
|
-
dedup(context_key, :date)
|
59
|
-
@definitions[context_key] = RegistryTypes::Date.new(formatter: formatter)
|
60
|
-
end
|
61
|
-
|
62
|
-
def hash(context_key, &definitions)
|
63
|
-
dedup(context_key, :hash)
|
64
|
-
@definitions[context_key] = RegistryTypes::Hash.new(&definitions)
|
65
|
-
end
|
66
|
-
|
67
|
-
def run(&definitions)
|
68
|
-
instance_eval(&definitions)
|
69
|
-
end
|
70
|
-
|
71
|
-
def dedup(key, type)
|
72
|
-
@definitions.include?(key) and
|
73
|
-
raise Registry::DuplicateDefinitionError, "Defining duplicate entry #{key} previously as #{@definitions[key]} and now as #{type}"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ContextualLogger
|
4
|
-
module Context
|
5
|
-
module RegistryTypes
|
6
|
-
class Number
|
7
|
-
attr_reader :formatter
|
8
|
-
|
9
|
-
def initialize(formatter: nil)
|
10
|
-
@formatter = formatter || :to_i
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_h
|
14
|
-
{ type: :number, formatter: formatter }
|
15
|
-
end
|
16
|
-
|
17
|
-
def format(value)
|
18
|
-
case formatter
|
19
|
-
when Proc
|
20
|
-
formatter.call(value)
|
21
|
-
else
|
22
|
-
value.send(formatter)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ContextualLogger
|
4
|
-
module Context
|
5
|
-
module RegistryTypes
|
6
|
-
class String
|
7
|
-
attr_reader :formatter
|
8
|
-
|
9
|
-
def initialize(formatter: nil)
|
10
|
-
@formatter = formatter || :to_s
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_h
|
14
|
-
{ type: :string, formatter: formatter }
|
15
|
-
end
|
16
|
-
|
17
|
-
def format(value)
|
18
|
-
case formatter
|
19
|
-
when Proc
|
20
|
-
formatter.call(value)
|
21
|
-
else
|
22
|
-
value.send(formatter)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|