injectedlogger 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -13
- data/lib/injectedlogger.rb +15 -5
- data/lib/injectedlogger/logger.rb +18 -0
- data/lib/injectedlogger/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dd3d22d80668b023317badf017352dd0f0abf70
|
4
|
+
data.tar.gz: d31bae1ac80c58e7bd389d7337c8fe0f16932cf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a32adcb1e875e2d82d57e4bc906f09e0f565e5233009e6e320b99a0b42d7e88f538584b77e20f00ac8b8107a529c31f47b347b36d70ae3761977e7b79d4bb1ac
|
7
|
+
data.tar.gz: 2e22f22c43ef4ad755edd660cbd4be03fe8326bf2fa7dd56fd5b9c4b5b67899fd4470bd143df46a52b15e017972b3b3ee23a5c118820f79f852c7041781c82dd
|
data/README.md
CHANGED
@@ -4,25 +4,19 @@ This gem can be used to inject a logger in your Ruby code.
|
|
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
|
|
7
|
-
## Usage
|
7
|
+
## Usage examples
|
8
8
|
|
9
|
-
|
10
|
-
logger = InjectedLogger::Logger.inject somelogger
|
11
|
-
raise 'No info :(' unless logger.level_info[:supported].include? :info
|
12
|
-
logger.info 'You now have a logger!'
|
13
|
-
```
|
14
|
-
|
15
|
-
or you can set-up a default injection for your logger in case no one else sets it up before you need to log something:
|
9
|
+
### On your code, which receives a logger from some foreign caller:
|
16
10
|
|
17
11
|
```ruby
|
18
12
|
module MyLogger
|
19
|
-
InjectedLogger.
|
13
|
+
InjectedLogger.use :debug, :info, :invented do
|
20
14
|
# this gets executed if no logger has been injected at use time
|
21
15
|
require 'logger'
|
22
|
-
{ logger: Logger.new(STDERR)
|
16
|
+
{ logger: Logger.new(STDERR) }
|
23
17
|
end
|
24
18
|
InjectedLogger.after_injection do |logger|
|
25
|
-
logger.
|
19
|
+
logger.prefix = '[myproject]'
|
26
20
|
end
|
27
21
|
end
|
28
22
|
|
@@ -30,7 +24,7 @@ class WantsLogging
|
|
30
24
|
include MyLogger
|
31
25
|
|
32
26
|
def some_method_needing_logging
|
33
|
-
logger.info '
|
27
|
+
logger.info 'some info'
|
34
28
|
end
|
35
29
|
end
|
36
30
|
|
@@ -38,11 +32,17 @@ class ThisAlsoWantsIt
|
|
38
32
|
include MyLogger
|
39
33
|
|
40
34
|
def some_other_method_with_debug_logging
|
41
|
-
logger.
|
35
|
+
logger.invented 'some invented info'
|
42
36
|
end
|
43
37
|
end
|
44
38
|
```
|
45
39
|
|
40
|
+
### On the code injecting a logger:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
InjectedLogger.inject somelogger
|
44
|
+
```
|
45
|
+
|
46
46
|
## Generating the gem
|
47
47
|
|
48
48
|
Both bundler and rspec are required to build the gem:
|
data/lib/injectedlogger.rb
CHANGED
@@ -6,7 +6,7 @@ module InjectedLogger
|
|
6
6
|
# inject a default logger in case no one has set one for you:
|
7
7
|
#
|
8
8
|
# module MyLogger
|
9
|
-
# InjectedLogger.
|
9
|
+
# InjectedLogger.use :info, :debug, :invented do
|
10
10
|
# require 'logger'
|
11
11
|
# # parameters are inject() params, none is required, but if
|
12
12
|
# # logger is not present, a default one will be used.
|
@@ -20,6 +20,12 @@ module InjectedLogger
|
|
20
20
|
# def some_method_needing_logging
|
21
21
|
# logger.info 'some info'
|
22
22
|
# end
|
23
|
+
#
|
24
|
+
# def some_method_with_invented_level_logging
|
25
|
+
# logger.invented do
|
26
|
+
# 'some invented logging'
|
27
|
+
# end
|
28
|
+
# end
|
23
29
|
# end
|
24
30
|
#
|
25
31
|
# This will only run the block passed to inject IFF there was no logger set
|
@@ -27,7 +33,7 @@ module InjectedLogger
|
|
27
33
|
# logger method is called, so that it does not 'require' anything if it is not
|
28
34
|
# needed. :)
|
29
35
|
|
30
|
-
def self.
|
36
|
+
def self.use(*required, on: nil, method_name: :logger, &blk)
|
31
37
|
if on.nil?
|
32
38
|
raise InjectedLogger::DefaultInjectionBlockMissing if blk.nil?
|
33
39
|
on = blk.binding.eval 'self'
|
@@ -37,13 +43,17 @@ module InjectedLogger
|
|
37
43
|
on.send :define_method, method_name do
|
38
44
|
# avoid recursion if someone calls logger in the block
|
39
45
|
on.send :remove_method, method_name
|
40
|
-
unless InjectedLogger
|
46
|
+
unless InjectedLogger.injected?
|
41
47
|
args = blk ? blk.call : nil
|
42
48
|
InjectedLogger.inject_logger args, required
|
43
49
|
end
|
44
50
|
required.uniq!
|
45
51
|
required -= InjectedLogger::Logger.level_info[:supported]
|
46
|
-
|
52
|
+
unless required.empty?
|
53
|
+
InjectedLogger::Logger.add_levels(*required)
|
54
|
+
required -= InjectedLogger::Logger.level_info[:supported]
|
55
|
+
raise InjectedLogger::UnsupportedLevels.new(required) unless required.empty?
|
56
|
+
end
|
47
57
|
on.send :define_method, method_name do
|
48
58
|
InjectedLogger::Logger
|
49
59
|
end
|
@@ -77,6 +87,6 @@ module InjectedLogger
|
|
77
87
|
args[:levels].push(required).flatten!
|
78
88
|
args[:levels].uniq!
|
79
89
|
end
|
80
|
-
InjectedLogger
|
90
|
+
InjectedLogger.inject(logger, args)
|
81
91
|
end
|
82
92
|
end
|
@@ -2,6 +2,18 @@ require 'injectedlogger/errors'
|
|
2
2
|
require 'injectedlogger/delegator'
|
3
3
|
|
4
4
|
module InjectedLogger
|
5
|
+
def self.injected?
|
6
|
+
Logger.injected?
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.inject(*args)
|
10
|
+
Logger.inject(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.inject!(*args)
|
14
|
+
Logger.inject!(*args)
|
15
|
+
end
|
16
|
+
|
5
17
|
module Logger
|
6
18
|
UNKNOWN = :unknown
|
7
19
|
LOGLEVELS = [:debug, :verbose, :notice, :info, :warn, :error, :critical, :fatal, :unknown]
|
@@ -39,6 +51,12 @@ module InjectedLogger
|
|
39
51
|
add_methods
|
40
52
|
end
|
41
53
|
|
54
|
+
def add_levels(*levels)
|
55
|
+
lvls = self.levels + levels
|
56
|
+
lvls.uniq!
|
57
|
+
self.levels = lvls
|
58
|
+
end
|
59
|
+
|
42
60
|
def fallback=(level)
|
43
61
|
set_fallback level
|
44
62
|
add_methods
|