contextual_logger 1.1.1 → 1.2.0.colin.1

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: a7d4ef0ffd9faf3c8994edbc8f96cf0fbac7d29d3c776c54228740cc0801ca39
4
- data.tar.gz: a423b940026c1830747c60c9c8bdd0eda732a148892ba40309ffd7888cebdd83
3
+ metadata.gz: 68939d6780d99c6d6a266aa6bcf10c4d457b27bb34bf2dc06b8faacf26aec1bd
4
+ data.tar.gz: c90bc720ec41a2d61a177243c815c109f16d631db1251ea03497d80bd2b6e22e
5
5
  SHA512:
6
- metadata.gz: 62cb70627e93a215057100deb1b7f04c409355f423b3e58ecd37d95268c2ce94bfb73633048fb6c6a83fd0dea4b7383d80bfdf27e91c7258eb36329d00d7de41
7
- data.tar.gz: 0be4154aaa62e04201d9c10ab12e199f5c59c387349b4ff28b73d86cc42ee82562a661ba1f11c8c42a8fc378410ce7171be84531093e90feaf4c6993afbbb62e
6
+ metadata.gz: a86dbe5c5f407d2affc7a4a58e237e0b61cb36db5dceb33db62bf3e01cb94e5f4e9863a3ac79765c02e5bb02d682246a854a71d5c102d390f1b6cb759faf5d79
7
+ data.tar.gz: 7376b31fbb12bce7a87f506a3c2f0ad89a5fe92c925cd9b469f176195ecfe21fa235a2a168ca9fcd43a42e7c7db7a56649aa5d02fa3d430ccc1108e710aa0ab7
@@ -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
@@ -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.1'
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.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: 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