contextual_logger 1.2.0.colin.4 → 1.2.0.pre.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: 7b8180f031cfc4c1facc22a0cfbbadf8970b01a1b7c1d18f974dbf54fd1712d5
4
- data.tar.gz: d310df13569303aa5f6964d1cf9f73583fcfcabcd5950f330b00a30f8eb151f5
3
+ metadata.gz: d5ee8cad9d62ca2d8aeec81f2626b56b4aa6b1db2c6b626518a4708d590ddb4c
4
+ data.tar.gz: 71be08f271be68de04f942abfa301a2a5ec54be54d0399bb01db8bcf52a07037
5
5
  SHA512:
6
- metadata.gz: e0c0350c9f36cf27071a7e1da1f42363979852304c4b029ca24d0a550c77bfb403815dc09420d5c8d9c164b003f8a22213e5d33216e1647ae63143a3b73447e9
7
- data.tar.gz: 4984642ce8b36e78c3f14f0415ac95d67dec97f9f5651f6c63deb715bbfa27134243f7286e09802146b6629df0bc1077c476ea3e3d5ad783779daa718aef032d
6
+ metadata.gz: 2b455d41a46223ea9c6e2583a7d03d81940743215c4eb1dd5977d6ffc8f8ffcb618ce27a3b975fed211987b592e0cfdbc1e691ceef155641764c5d8938da0679
7
+ data.tar.gz: 6600b357b1dc7b25ee8e746f17280a7e74ac184b38a03340f48e835659259135321a0c0ff0d7c387a7b06b9804b82ad63f8b76a911790a6fb5c2f99a61c0cfdc
@@ -0,0 +1,28 @@
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
@@ -19,10 +19,6 @@ 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
-
26
22
  def level
27
23
  @override_level || @logger.level
28
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContextualLogger
4
- VERSION = '1.2.0.colin.4'
4
+ VERSION = '1.2.0.pre.1'
5
5
  end
@@ -4,8 +4,7 @@ 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'
8
- require_relative './contextual_logger/context_handler'
7
+ require_relative './contextual_logger/context/handler'
9
8
 
10
9
  module ContextualLogger
11
10
  LOG_LEVEL_NAMES_TO_SEVERITY =
@@ -44,40 +43,31 @@ module ContextualLogger
44
43
  end
45
44
 
46
45
  module LoggerMixin
47
- include Context
48
-
49
46
  delegate :register_secret, :register_secret_regex, to: :redactor
50
47
 
51
- def global_context
52
- @global_context ||= {}.freeze
53
- end
54
-
55
48
  def global_context=(context)
56
- @global_context = context.freeze
57
- end
58
-
59
- def current_context
60
- current_context_override || global_context
49
+ Context::Handler.new(context).set!
61
50
  end
62
51
 
63
- # TODO: Deprecate current_context_for_thread in v2.0.
64
- alias current_context_for_thread current_context
65
-
66
52
  def with_context(context)
67
- previous_context_override = current_context_override
68
- self.current_context_override = current_context.deep_merge(context)
53
+ context_handler = Context::Handler.new(current_context_for_thread.deep_merge(context))
54
+ context_handler.set!
69
55
  if block_given?
70
56
  begin
71
57
  yield
72
58
  ensure
73
- self.current_context_override = previous_context_override
59
+ context_handler.reset!
74
60
  end
75
61
  else
76
- # If no block given, return context handler to the caller so they can call reset! themselves.
77
- ContextHandler.new(self, previous_context_override)
62
+ # If no block given, the context handler is returned to the caller so they can handle reset! themselves.
63
+ context_handler
78
64
  end
79
65
  end
80
66
 
67
+ def current_context_for_thread
68
+ Context::Handler.current_context
69
+ end
70
+
81
71
  # In the methods generated below, we assume that presence of context means new code that is
82
72
  # aware of ContextualLogger...and that that code never uses progname.
83
73
  # This is important because we only get 3 args total (not including &block) passed to `add`,
@@ -127,7 +117,7 @@ module ContextualLogger
127
117
  message = arg1
128
118
  progname = arg2 || @progname
129
119
  end
130
- write_entry_to_log(severity, Time.now, progname, message, context: current_context.deep_merge(context))
120
+ write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(context))
131
121
  end
132
122
 
133
123
  true
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.2.0.colin.4
4
+ version: 1.2.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: 2023-09-02 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -45,8 +45,7 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - lib/contextual_logger.rb
48
- - lib/contextual_logger/context.rb
49
- - lib/contextual_logger/context_handler.rb
48
+ - lib/contextual_logger/context/handler.rb
50
49
  - lib/contextual_logger/logger_with_context.rb
51
50
  - lib/contextual_logger/overrides/active_support/tagged_logging/formatter.rb
52
51
  - lib/contextual_logger/redactor.rb
@@ -1,18 +0,0 @@
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_override
11
- Thread.current[thread_context_for_logger_instance]
12
- end
13
-
14
- def current_context_override=(context_override)
15
- Thread.current[thread_context_for_logger_instance] = context_override.freeze
16
- end
17
- end
18
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ContextualLogger
4
- class ContextHandler
5
- def initialize(instance, previous_context_override)
6
- @instance = instance
7
- @previous_context_override = previous_context_override
8
- end
9
-
10
- def reset!
11
- @instance.current_context_override = @previous_context_override
12
- end
13
- end
14
- end