contextual_logger 0.1.0
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 +7 -0
- data/lib/contextual_logger.rb +94 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 792d027e06730423ee08c1321a92d05b75d9c9a2
|
4
|
+
data.tar.gz: 4feda11871b919feeda59f2a94a7bd48e18412f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9dc835d30281ef8f52135a5040dc7fa708a1b22d4e20a5d0d4e1e5d5560afd2cc51decd0cd3e238a8ef3160dd4c50f670a6d4b70bd7cf14b127da62574b5b9cc
|
7
|
+
data.tar.gz: 8b59b651971148697c2f2e8cdc633278a25667bb00e7c54186cfe8d8f8f0a12556a6eb143e117a25944dcda1b5d91c15fafedd915dd753ae7a74f92437cc9f3b
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module ContextualLogger
|
6
|
+
def self.new(logger)
|
7
|
+
logger.extend(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def global_context=(context)
|
11
|
+
Thread.current[THREAD_CONTEXT_NAMESPACE] = context
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_context(context)
|
15
|
+
previous_context = Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
|
16
|
+
Thread.current[THREAD_CONTEXT_NAMESPACE] = previous_context.merge(context)
|
17
|
+
yield if block_given?
|
18
|
+
ensure
|
19
|
+
Thread.current[THREAD_CONTEXT_NAMESPACE] = previous_context
|
20
|
+
end
|
21
|
+
|
22
|
+
def current_context_for_thread
|
23
|
+
Thread.current[THREAD_CONTEXT_NAMESPACE] || {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def format_message(severity, timestamp, progname, message, context)
|
27
|
+
message_with_context = message_with_context(context, message, severity, timestamp, progname)
|
28
|
+
|
29
|
+
if @formatter
|
30
|
+
@formatter.call(severity, timestamp, progname, message_with_context)
|
31
|
+
else
|
32
|
+
"#{message_with_context.to_json}\n"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def debug(progname = nil, **extra_context, &block)
|
37
|
+
add(Logger::Severity::DEBUG, nil, progname, extra_context, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def info(progname = nil, **extra_context, &block)
|
41
|
+
add(Logger::Severity::INFO, nil, progname, extra_context, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def warn(progname = nil, **extra_context, &block)
|
45
|
+
add(Logger::Severity::WARN, nil, progname, extra_context, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def error(progname = nil, **extra_context, &block)
|
49
|
+
add(Logger::Severity::ERROR, nil, progname, extra_context, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
def fatal(progname = nil, **extra_context, &block)
|
53
|
+
add(Logger::Severity::FATAL, nil, progname, extra_context, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
def unknown(progname = nil, **extra_context, &block)
|
57
|
+
add(Logger::Severity::UNKNOWN, nil, progname, extra_context, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def add(severity, message = nil, progname = nil, extra_context = nil)
|
61
|
+
severity ||= UNKNOWN
|
62
|
+
if @logdev.nil? || (severity < @level)
|
63
|
+
return true
|
64
|
+
end
|
65
|
+
progname ||= @progname
|
66
|
+
if message.nil?
|
67
|
+
if block_given?
|
68
|
+
message = yield
|
69
|
+
else
|
70
|
+
message = progname
|
71
|
+
progname = @progname
|
72
|
+
end
|
73
|
+
end
|
74
|
+
write_entry_to_log(severity, Time.now, progname, message, current_context_for_thread.merge(extra_context))
|
75
|
+
true
|
76
|
+
end
|
77
|
+
|
78
|
+
def write_entry_to_log(severity, timestamp, progname, message, context)
|
79
|
+
@logdev.write(format_message(format_severity(severity), timestamp, progname, message, context))
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
THREAD_CONTEXT_NAMESPACE = 'ContextualLoggerCurrentLoggingContext'
|
85
|
+
|
86
|
+
def message_with_context(context, message, severity, timestamp, progname)
|
87
|
+
context.merge(
|
88
|
+
message: message,
|
89
|
+
severity: severity,
|
90
|
+
timestamp: timestamp,
|
91
|
+
progname: progname
|
92
|
+
)
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: contextual_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Ebentier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A way to add context to the logs you have
|
28
|
+
email: jebentier@invoca.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/contextual_logger.rb
|
34
|
+
homepage: https://rubygems.org/gems/contextual_logger
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
source_code_uri: https://github.com/Invoca/contextual_logger
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.6.13
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Add context to your logger
|
59
|
+
test_files: []
|