contextual_logger 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/contextual_logger.rb +13 -9
- data/lib/contextual_logger/context/handler.rb +28 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2005c27e3db3c69146eeae90c5dd89f75f8ad56c
|
4
|
+
data.tar.gz: b7cb42a1aee422df6e2135d35d67c81202f944b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 877c0ab113f3d220c87670739c7fb1a0a242f5ebd73c54fcc2eb12227747a47d041c222394a1c2485ce22e8d40255362a9956922cd3b0a8328ca4eead73f2c3e
|
7
|
+
data.tar.gz: 3c201cbe9719e9f19c31e7f3f8c3960ba37eb8099df2e808a7704b27a728ca9d2640173bd02afc8165a4cc53ef9a72bda3d5fc3cc3b89ac5933f4680f6c072fd
|
data/lib/contextual_logger.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support'
|
3
4
|
require 'json'
|
5
|
+
require_relative './contextual_logger/context/handler'
|
4
6
|
|
5
7
|
module ContextualLogger
|
6
8
|
def self.new(logger)
|
@@ -8,19 +10,23 @@ module ContextualLogger
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def global_context=(context)
|
11
|
-
|
13
|
+
ContextualLogger::Context::Handler.new(context).set!
|
12
14
|
end
|
13
15
|
|
14
16
|
def with_context(context)
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
context_handler = ContextualLogger::Context::Handler.new(current_context_for_thread.deep_merge(context))
|
18
|
+
context_handler.set!
|
19
|
+
if block_given?
|
20
|
+
yield
|
21
|
+
else
|
22
|
+
context_handler
|
23
|
+
end
|
18
24
|
ensure
|
19
|
-
|
25
|
+
context_handler.reset! if block_given?
|
20
26
|
end
|
21
27
|
|
22
28
|
def current_context_for_thread
|
23
|
-
|
29
|
+
ContextualLogger::Context::Handler.current_context
|
24
30
|
end
|
25
31
|
|
26
32
|
def format_message(severity, timestamp, progname, message, context)
|
@@ -71,7 +77,7 @@ module ContextualLogger
|
|
71
77
|
progname = @progname
|
72
78
|
end
|
73
79
|
end
|
74
|
-
write_entry_to_log(severity, Time.now, progname, message, current_context_for_thread.
|
80
|
+
write_entry_to_log(severity, Time.now, progname, message, current_context_for_thread.deep_merge(extra_context || {}))
|
75
81
|
true
|
76
82
|
end
|
77
83
|
|
@@ -81,8 +87,6 @@ module ContextualLogger
|
|
81
87
|
|
82
88
|
private
|
83
89
|
|
84
|
-
THREAD_CONTEXT_NAMESPACE = 'ContextualLoggerCurrentLoggingContext'
|
85
|
-
|
86
90
|
def message_with_context(context, message, severity, timestamp, progname)
|
87
91
|
context.merge(
|
88
92
|
message: message,
|
@@ -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
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contextual_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Ebentier
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: A way to add context to the logs you have
|
28
42
|
email: jebentier@invoca.com
|
29
43
|
executables: []
|
@@ -31,6 +45,7 @@ extensions: []
|
|
31
45
|
extra_rdoc_files: []
|
32
46
|
files:
|
33
47
|
- lib/contextual_logger.rb
|
48
|
+
- lib/contextual_logger/context/handler.rb
|
34
49
|
- lib/contextual_logger/overrides/active_support/tagged_logging/formatter.rb
|
35
50
|
homepage: https://rubygems.org/gems/contextual_logger
|
36
51
|
licenses:
|