contextual_logger 1.1.1 → 1.2.0.colin.2

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: a7d4ef0ffd9faf3c8994edbc8f96cf0fbac7d29d3c776c54228740cc0801ca39
4
- data.tar.gz: a423b940026c1830747c60c9c8bdd0eda732a148892ba40309ffd7888cebdd83
3
+ metadata.gz: fa81557c8d0035dc91b9285be3a429b5c3bef9d27842f62306a827b3a842d284
4
+ data.tar.gz: f63ece3db1c77acda0f05ea7af4dc581aefff65bb62464977a8b9a645090fe9f
5
5
  SHA512:
6
- metadata.gz: 62cb70627e93a215057100deb1b7f04c409355f423b3e58ecd37d95268c2ce94bfb73633048fb6c6a83fd0dea4b7383d80bfdf27e91c7258eb36329d00d7de41
7
- data.tar.gz: 0be4154aaa62e04201d9c10ab12e199f5c59c387349b4ff28b73d86cc42ee82562a661ba1f11c8c42a8fc378410ce7171be84531093e90feaf4c6993afbbb62e
6
+ metadata.gz: 5dbfc7e625e0b8e9b7111371da8368194d8d628192f91955cc98807fb0e8ecc67e10473137c1333513376d233c112a990c8fe8e5506741322995433c4f75f62f
7
+ data.tar.gz: 966f798d247c5cadaa32e2269830153f7280e0ad015554db4f065c7a5f5729f5f39b3f443dde6f8f4659797da218ede4af320ac278723651099c2e8f2495e877
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ContextualLogger
4
+ module Context
5
+ def thread_context_for_logger_instance
6
+ # We include the object_id here to make these thread/fiber locals unique per logger instance.
7
+ @thread_context_for_logger_instance ||= "ContextualLogger::Context.context_for_#{object_id}".to_sym
8
+ end
9
+
10
+ def current_context
11
+ Thread.current[thread_context_for_logger_instance]
12
+ end
13
+
14
+ def current_context=(context)
15
+ Thread.current[thread_context_for_logger_instance] = context.freeze
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ContextualLogger
4
+ class ContextHandler
5
+ def initialize(instance, previous_context)
6
+ @instance = instance
7
+ @previous_context = previous_context
8
+ end
9
+
10
+ def reset!
11
+ @instance.current_context = @previous_context
12
+ end
13
+ end
14
+ end
@@ -19,6 +19,10 @@ module ContextualLogger
19
19
  @merged_context_cache = {} # so we don't have to merge every time
20
20
  end
21
21
 
22
+ def global_context
23
+ @logger.global_context
24
+ end
25
+
22
26
  def level
23
27
  @override_level || @logger.level
24
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContextualLogger
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0.colin.2'
5
5
  end
@@ -4,7 +4,8 @@ require 'active_support'
4
4
  require 'active_support/core_ext/module/delegation'
5
5
  require 'json'
6
6
  require_relative './contextual_logger/redactor'
7
- require_relative './contextual_logger/context/handler'
7
+ require_relative './contextual_logger/context'
8
+ require_relative './contextual_logger/context_handler'
8
9
 
9
10
  module ContextualLogger
10
11
  LOG_LEVEL_NAMES_TO_SEVERITY =
@@ -43,31 +44,37 @@ module ContextualLogger
43
44
  end
44
45
 
45
46
  module LoggerMixin
47
+ include Context
48
+
46
49
  delegate :register_secret, :register_secret_regex, to: :redactor
47
50
 
51
+ def global_context
52
+ @global_context ||= {}.freeze
53
+ end
54
+
48
55
  def global_context=(context)
49
- Context::Handler.new(context).set!
56
+ @global_context = context.freeze
57
+ end
58
+
59
+ def current_context_for_thread
60
+ current_context || global_context
50
61
  end
51
62
 
52
63
  def with_context(context)
53
- context_handler = Context::Handler.new(current_context_for_thread.deep_merge(context))
54
- context_handler.set!
64
+ previous_context = current_context_for_thread
65
+ self.current_context = previous_context.deep_merge(context)
55
66
  if block_given?
56
67
  begin
57
68
  yield
58
69
  ensure
59
- context_handler.reset!
70
+ self.current_context = previous_context
60
71
  end
61
72
  else
62
- # If no block given, the context handler is returned to the caller so they can handle reset! themselves.
63
- context_handler
73
+ # If no block given, return context handler to the caller so they can call reset! themselves.
74
+ ContextHandler.new(self, previous_context)
64
75
  end
65
76
  end
66
77
 
67
- def current_context_for_thread
68
- Context::Handler.current_context
69
- end
70
-
71
78
  # In the methods generated below, we assume that presence of context means new code that is
72
79
  # aware of ContextualLogger...and that that code never uses progname.
73
80
  # This is important because we only get 3 args total (not including &block) passed to `add`,
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: 1.1.1
4
+ version: 1.2.0.colin.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Ebentier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-20 00:00:00.000000000 Z
11
+ date: 2023-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -45,7 +45,8 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - lib/contextual_logger.rb
48
- - lib/contextual_logger/context/handler.rb
48
+ - lib/contextual_logger/context.rb
49
+ - lib/contextual_logger/context_handler.rb
49
50
  - lib/contextual_logger/logger_with_context.rb
50
51
  - lib/contextual_logger/overrides/active_support/tagged_logging/formatter.rb
51
52
  - lib/contextual_logger/redactor.rb
@@ -67,9 +68,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
68
  version: '0'
68
69
  required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  requirements:
70
- - - ">="
71
+ - - ">"
71
72
  - !ruby/object:Gem::Version
72
- version: '0'
73
+ version: 1.3.1
73
74
  requirements: []
74
75
  rubygems_version: 3.1.6
75
76
  signing_key:
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ContextualLogger
4
- module Context
5
- class Handler
6
- THREAD_CONTEXT_NAMESPACE = 'ContextualLoggerCurrentLoggingContext'
7
-
8
- attr_reader :previous_context, :context
9
-
10
- def self.current_context
11
- Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
12
- end
13
-
14
- def initialize(context, previous_context: nil)
15
- @previous_context = previous_context || self.class.current_context
16
- @context = context
17
- end
18
-
19
- def set!
20
- Thread.current[THREAD_CONTEXT_NAMESPACE] = context
21
- end
22
-
23
- def reset!
24
- Thread.current[THREAD_CONTEXT_NAMESPACE] = previous_context
25
- end
26
- end
27
- end
28
- end