contextual_logger 0.11.0 → 0.12.0.pre.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c5fcdf15e253eb8abb46dde530238633bd584f151e0282526dcfce923b4dc50
4
- data.tar.gz: da4e10e4a778d244b2c36640ec4600bc76e4357fd4b0ddfbfef362c2276bbd5a
3
+ metadata.gz: c2ef105e4dfaf32e5e215d83e05ac6ba4f5c67a32cb11e214598a668b4afd34e
4
+ data.tar.gz: d8fa3b949bf2880a1992f6bce1fcfd6b6b59ec4aa46340d9752c8b27fc7dd6cc
5
5
  SHA512:
6
- metadata.gz: c575941e59fe7196d27528ea0733bd841eb47e45914f8b74add858227fc55161c2cbc2837653033f80ddfea3c3768538c137392d0e3a2b9f64c344982fb281ff
7
- data.tar.gz: 199b5c2446a3fe1e6730b352dd23e93c0f9a52e04fffc335b28c6b26e0a2f5e3fe658b9949f57f87b3adba952e2b27ebc493e05dc5a3b2944baefcea0203e099
6
+ metadata.gz: 3df6d16bf7ffc7cc36bb0f95103222f1791a228ee4e1034ef883261bdaa10ac5d16c57fafa6cdd58d524fee2ebec5c86fab5b2dcc3df05eaf2d35acea1f00eed
7
+ data.tar.gz: e1db9f65be3eb35690cea59ceee623188b40f03377b7a467d12d2644ed8a957246afb70a43eb736a639d541ecdf093d7832f909b0878d1e87c41d889e9be44ea
@@ -5,6 +5,7 @@ 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'
8
9
 
9
10
  module ContextualLogger
10
11
  LOG_LEVEL_NAMES_TO_SEVERITY =
@@ -45,12 +46,22 @@ module ContextualLogger
45
46
  module LoggerMixin
46
47
  delegate :register_secret, to: :redactor
47
48
 
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
+
48
54
  def global_context=(context)
49
- Context::Handler.new(context).set!
55
+ Context::Handler.new(context_registry.format(context)).set!
50
56
  end
51
57
 
52
58
  def with_context(context)
53
- context_handler = Context::Handler.new(current_context_for_thread.deep_merge(context))
59
+ context_handler = Context::Handler.new(
60
+ current_context_for_thread.deep_merge(
61
+ context_registry.format(context)
62
+ )
63
+ )
64
+
54
65
  context_handler.set!
55
66
  if block_given?
56
67
  begin
@@ -117,7 +128,7 @@ module ContextualLogger
117
128
  message = arg1
118
129
  progname = arg2 || @progname
119
130
  end
120
- write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(context))
131
+ write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(context_registry.format(context)))
121
132
  end
122
133
 
123
134
  true
@@ -133,6 +144,13 @@ module ContextualLogger
133
144
 
134
145
  private
135
146
 
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
+
136
154
  def redactor
137
155
  @redactor ||= Redactor.new
138
156
  end
@@ -7,8 +7,10 @@ module ContextualLogger
7
7
 
8
8
  attr_reader :previous_context, :context
9
9
 
10
- def self.current_context
11
- Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
10
+ class << self
11
+ def current_context
12
+ Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
13
+ end
12
14
  end
13
15
 
14
16
  def initialize(context, previous_context: nil)
@@ -0,0 +1,73 @@
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
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,78 @@
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
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,28 @@
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContextualLogger
4
- VERSION = '0.11.0'
4
+ VERSION = '0.12.0.pre.1'
5
5
  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: 0.11.0
4
+ version: 0.12.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Ebentier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -46,6 +46,12 @@ 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
49
55
  - lib/contextual_logger/logger_with_context.rb
50
56
  - lib/contextual_logger/overrides/active_support/tagged_logging/formatter.rb
51
57
  - lib/contextual_logger/redactor.rb
@@ -67,9 +73,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
73
  version: '0'
68
74
  required_rubygems_version: !ruby/object:Gem::Requirement
69
75
  requirements:
70
- - - ">="
76
+ - - ">"
71
77
  - !ruby/object:Gem::Version
72
- version: '0'
78
+ version: 1.3.1
73
79
  requirements: []
74
80
  rubygems_version: 3.0.3
75
81
  signing_key: