injectedlogger 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/lib/injectedlogger/logger.rb +95 -81
- data/lib/injectedlogger/version.rb +1 -1
- data/lib/injectedlogger.rb +13 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b509e96629351ab9e5dad7429cff1942e09a12a6
|
4
|
+
data.tar.gz: a54f79bdf60b07015830f44749b8de05127a47e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b1cb6ab011b7a7017548f4a271417a97d3458488190e68338465cec18d28eb90b6cd9c48f6c9a39f2635f1b8ef214e6ba9f3ca8663cdb098ddddbc8990af62c
|
7
|
+
data.tar.gz: 426effc3a7dc5e304eb3f8fa43d62c9ba7ca6827808e1cfe6d281e1d6774cc40757894b7fa234697d5f3336c6a93eaebb996f11426632c5dc862c285b3e2af9d
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# A logger injection gem
|
2
2
|
|
3
|
-
This gem can be used to inject
|
3
|
+
This gem can be used to inject several loggers in different parts of your Ruby project.
|
4
4
|
|
5
5
|
It will try to support as many methods and levels as the underlying object supports, and fall back to a supported level in case some levels are not available
|
6
6
|
|
@@ -40,7 +40,13 @@ end
|
|
40
40
|
### On the code injecting a logger:
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
InjectedLogger.inject somelogger
|
43
|
+
InjectedLogger.inject somelogger, on: MyLogger
|
44
|
+
```
|
45
|
+
|
46
|
+
### In case some other dependency needs a logger:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
InjectedLogger.inject somelogger, prefix: '[other]', on: Other::Logger
|
44
50
|
```
|
45
51
|
|
46
52
|
## Generating the gem
|
@@ -2,115 +2,129 @@ require 'injectedlogger/errors'
|
|
2
2
|
require 'injectedlogger/delegator'
|
3
3
|
|
4
4
|
module InjectedLogger
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
def self.logger
|
7
|
+
@logger ||= Hash.new { |h, k| h[k] = Logger.new }
|
8
|
+
end
|
9
|
+
private_class_method :logger
|
10
|
+
|
11
|
+
def self.injected?(on:)
|
12
|
+
logger[on].injected?
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.inject(*args, on:, **options)
|
16
|
+
logger[on].inject(*args, **options)
|
7
17
|
end
|
8
18
|
|
9
|
-
def self.inject(*args)
|
10
|
-
|
19
|
+
def self.inject!(*args, on:, **options)
|
20
|
+
logger[on].inject!(*args, **options)
|
11
21
|
end
|
12
22
|
|
13
|
-
def self.
|
14
|
-
|
23
|
+
def self.after_injection(&blk)
|
24
|
+
on = blk.binding.eval 'self'
|
25
|
+
logger[on].after_hook = blk
|
15
26
|
end
|
16
27
|
|
17
|
-
|
28
|
+
class Logger
|
18
29
|
UNKNOWN = :unknown
|
19
30
|
LOGLEVELS = [:debug, :verbose, :notice, :info, :warn, :error, :critical, :fatal, :unknown]
|
20
31
|
|
21
|
-
|
22
|
-
|
32
|
+
attr_reader :prefix, :levels, :level_info, :fallback
|
33
|
+
attr_accessor :after_hook
|
23
34
|
|
24
|
-
|
25
|
-
|
26
|
-
|
35
|
+
def injected?
|
36
|
+
not logger.nil?
|
37
|
+
end
|
27
38
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
inject! logger_obj, levels: levels, fallback: fallback
|
39
|
+
def inject(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
|
40
|
+
if logger and logger != logger_obj
|
41
|
+
raise InUse, "#{self} was already using logger #{logger}"
|
33
42
|
end
|
43
|
+
inject! logger_obj, levels: levels, fallback: fallback
|
44
|
+
end
|
34
45
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
46
|
+
def inject!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
|
47
|
+
self.logger = logger_obj
|
48
|
+
set_prefix prefix
|
49
|
+
set_levels levels
|
50
|
+
set_fallback fallback
|
51
|
+
add_methods
|
52
|
+
self
|
53
|
+
end
|
43
54
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
55
|
+
def prefix=(prefix)
|
56
|
+
set_prefix prefix
|
57
|
+
add_methods
|
58
|
+
end
|
48
59
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
60
|
+
def levels=(levels)
|
61
|
+
set_levels(levels)
|
62
|
+
add_methods
|
63
|
+
end
|
53
64
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
65
|
+
def add_levels(*levels)
|
66
|
+
lvls = self.levels + levels
|
67
|
+
lvls.uniq!
|
68
|
+
self.levels = lvls
|
69
|
+
end
|
59
70
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
71
|
+
def fallback=(level)
|
72
|
+
set_fallback level
|
73
|
+
add_methods
|
74
|
+
end
|
64
75
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
76
|
+
def method_missing(method, *args, &blk)
|
77
|
+
logger.send method, *args, &blk
|
78
|
+
rescue NoMethodError
|
79
|
+
super
|
80
|
+
end
|
70
81
|
|
71
|
-
|
72
|
-
|
73
|
-
|
82
|
+
def respond_to_missing?(method, priv = false)
|
83
|
+
logger.respond_to?(method, priv) || super
|
84
|
+
end
|
74
85
|
|
75
|
-
|
86
|
+
private
|
76
87
|
|
77
|
-
|
78
|
-
|
88
|
+
attr_accessor :logger
|
89
|
+
attr_writer :level_info
|
79
90
|
|
80
|
-
|
81
|
-
|
82
|
-
|
91
|
+
def set_prefix(prefix)
|
92
|
+
@prefix = prefix
|
93
|
+
end
|
83
94
|
|
84
|
-
|
85
|
-
|
86
|
-
|
95
|
+
def set_levels(levels)
|
96
|
+
@levels = levels
|
97
|
+
end
|
87
98
|
|
88
|
-
|
89
|
-
|
90
|
-
|
99
|
+
def set_fallback(level)
|
100
|
+
@fallback = level
|
101
|
+
end
|
91
102
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
100
|
-
end
|
103
|
+
def add_methods
|
104
|
+
old_levels = level_info ? level_info[:supported] : []
|
105
|
+
self.level_info = InjectedLogger::Delegator.delegate_levels(
|
106
|
+
from: logger, on: self, prefix: prefix, extra_levels: self.levels,
|
107
|
+
old_levels: old_levels, fallback: fallback)
|
108
|
+
set_levels(level_info[:supported])
|
109
|
+
end
|
101
110
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
" using #{level_info[:fallback].upcase} severity"
|
106
|
-
elsif level_info[:nonnative].any?
|
107
|
-
"unsupported log levels #{level_info[:nonnative].join ', '}"
|
108
|
-
else
|
109
|
-
nil
|
110
|
-
end
|
111
|
-
send level_info[:info], message if message
|
111
|
+
def ready
|
112
|
+
level_info[:supported].tap do
|
113
|
+
info_message(level_info) if level_info[:info]
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
117
|
+
def info_message(level_info)
|
118
|
+
message = if level_info[:fallback]
|
119
|
+
"non-native log levels #{level_info[:nonnative].join ', '} emulated" \
|
120
|
+
" using #{level_info[:fallback].upcase} severity"
|
121
|
+
elsif level_info[:nonnative].any?
|
122
|
+
"unsupported log levels #{level_info[:nonnative].join ', '}"
|
123
|
+
else
|
124
|
+
nil
|
125
|
+
end
|
126
|
+
send level_info[:info], message if message
|
127
|
+
end
|
128
|
+
|
115
129
|
end
|
116
130
|
end
|
data/lib/injectedlogger.rb
CHANGED
@@ -43,34 +43,27 @@ module InjectedLogger
|
|
43
43
|
on.send :define_method, method_name do
|
44
44
|
# avoid recursion if someone calls logger in the block
|
45
45
|
on.send :remove_method, method_name
|
46
|
-
unless InjectedLogger.injected?
|
46
|
+
unless InjectedLogger.injected? on: on
|
47
47
|
args = blk ? blk.call : nil
|
48
|
-
InjectedLogger.inject_logger args, required
|
48
|
+
InjectedLogger.inject_logger args, required, on: on
|
49
49
|
end
|
50
|
+
thislogger = InjectedLogger.send(:logger).[](on)
|
50
51
|
required.uniq!
|
51
|
-
required -=
|
52
|
+
required -= thislogger.level_info[:supported]
|
52
53
|
unless required.empty?
|
53
|
-
|
54
|
-
required -=
|
54
|
+
thislogger.add_levels(*required)
|
55
|
+
required -= thislogger.level_info[:supported]
|
55
56
|
raise InjectedLogger::UnsupportedLevels.new(required) unless required.empty?
|
56
57
|
end
|
57
58
|
on.send :define_method, method_name do
|
58
|
-
|
59
|
+
thislogger
|
59
60
|
end
|
60
|
-
|
61
|
-
|
61
|
+
thislogger.after_hook.call(thislogger) if thislogger.after_hook
|
62
|
+
thislogger.send :ready
|
63
|
+
thislogger
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
65
|
-
def self.after_injection(&blk)
|
66
|
-
self.after_hook = blk
|
67
|
-
end
|
68
|
-
|
69
|
-
class << self
|
70
|
-
attr_accessor :after_hook
|
71
|
-
private :after_hook=
|
72
|
-
end
|
73
|
-
|
74
67
|
private
|
75
68
|
|
76
69
|
def self.default_logger
|
@@ -78,7 +71,7 @@ module InjectedLogger
|
|
78
71
|
{ logger: ::Logger.new(STDERR) }
|
79
72
|
end
|
80
73
|
|
81
|
-
def self.inject_logger(args, required)
|
74
|
+
def self.inject_logger(args, required, on:)
|
82
75
|
args ||= {}
|
83
76
|
args = default_logger.merge(args) unless args.has_key? :logger
|
84
77
|
logger = args.delete :logger
|
@@ -87,6 +80,7 @@ module InjectedLogger
|
|
87
80
|
args[:levels].push(required).flatten!
|
88
81
|
args[:levels].uniq!
|
89
82
|
end
|
90
|
-
|
83
|
+
args[:on] = on
|
84
|
+
InjectedLogger.inject(logger, **args)
|
91
85
|
end
|
92
86
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: injectedlogger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alejandro Martinez Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|