injectedlogger 0.0.9 → 0.0.10
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 +4 -4
- data/README.md +85 -23
- data/lib/injectedlogger/logger.rb +1 -1
- data/lib/injectedlogger/version.rb +1 -1
- data/lib/injectedlogger.rb +8 -6
- 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: f16981ff8dc874e0d132b20da815f7d11b5ccd17
|
4
|
+
data.tar.gz: b10b69e4c143ab00dfb5195bcbefb2e646eafdd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8a3682add57e43dd21f8f76211b0181aaec5c9b6680667f10ca26cfd2195c61d647014c31de52721a7fb4429dbfea4aaaabb1e0675b23d1b9d65ac8751329eb
|
7
|
+
data.tar.gz: 020bf6172f45482b1c0e2e69ecc04a10248659d18521f1e6832a4c2b204e9799861739fa9abdca50f151806838da552e0b9cf11b93e2f592c9c04ccc7a081a84
|
data/README.md
CHANGED
@@ -1,54 +1,116 @@
|
|
1
|
+
[](http://badge.fury.io/rb/injectedlogger)
|
2
|
+
|
1
3
|
# A logger injection gem
|
2
4
|
|
3
5
|
This gem can be used to inject several loggers in different parts of your Ruby project.
|
4
6
|
|
5
7
|
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
8
|
|
7
|
-
##
|
9
|
+
## How to use it
|
10
|
+
|
11
|
+
You have to declare the log levels that your code will be using, and
|
12
|
+
InjectedLogger will make sure that the underlying object supports them.
|
13
|
+
|
14
|
+
The user of your code will just pass the logger and refer to either a module of
|
15
|
+
yours or a symbolic name or string.
|
16
|
+
|
17
|
+
Suppose we have two projects, one of which has the other as dependency, and has
|
18
|
+
a logger available. The other project wants to log whatever it does using a
|
19
|
+
logger injected (provided) by the first project.
|
8
20
|
|
9
|
-
### On
|
21
|
+
### On the code injecting the logger:
|
10
22
|
|
23
|
+
Inject our logger on `Dependency::Logger`, which is the module used by Dependency
|
24
|
+
for logging:
|
11
25
|
```ruby
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
26
|
+
InjectedLogger.inject mylogger, on: Dependency::Logger
|
27
|
+
```
|
28
|
+
|
29
|
+
Inject our logger on whatever is identified by `'dependency-logger'`:
|
30
|
+
```ruby
|
31
|
+
InjectedLogger.inject mylogger, on: 'dependency-logger'
|
32
|
+
```
|
33
|
+
|
34
|
+
Inject our logger on `'dependency-logger'`, and have it provide `:debug`, `:info`
|
35
|
+
and `:invented` log levels, with whatever of those being actually unsupported by
|
36
|
+
`mylogger` emulated with `:info`:
|
37
|
+
```ruby
|
38
|
+
InjectedLogger.inject mylogger, levels: [:debug, :info, :invented], fallback: :info, on: 'dependency-logger'
|
39
|
+
```
|
40
|
+
|
41
|
+
Inject our logger on `Dependency::Logger` and prefix the messages with `'[logger-for-dep]'`:
|
42
|
+
```ruby
|
43
|
+
InjectedLogger.inject mylogger, prefix: '[logger-for-dep]', on: Dependency::Logger
|
44
|
+
```
|
45
|
+
|
46
|
+
### On the code where you want a logger injected:
|
47
|
+
|
48
|
+
This sets up a module with a `logger` method identified as `'dependency-logger'`
|
49
|
+
and declaring requirements of `:debug`, `:info`, and `:invented` log levels. In
|
50
|
+
case no logger gets injected by the time `logger` is needed, the standard Ruby
|
51
|
+
logger with output on `STDERR` will be used. After a logger is finally available,
|
52
|
+
this also makes sure the prefix for each message is `'[dependency]'` (overriding
|
53
|
+
what the injector set up):
|
54
|
+
```ruby
|
55
|
+
module Dependency
|
56
|
+
module Logger
|
57
|
+
InjectedLogger.use :debug, :info, :invented, as: 'dependency-logger' do
|
58
|
+
# this gets executed if no logger has been injected at use time
|
59
|
+
require 'logger'
|
60
|
+
{ logger: Logger.new(STDERR) } # optionally add parameters for `inject`
|
61
|
+
end
|
62
|
+
InjectedLogger.after_injection on: 'dependency-logger' do |logger|
|
63
|
+
logger.prefix = '[dependency]'
|
64
|
+
end
|
20
65
|
end
|
21
66
|
end
|
67
|
+
```
|
22
68
|
|
69
|
+
Now you can use your module elsewhere in your project:
|
70
|
+
```ruby
|
23
71
|
class WantsLogging
|
24
|
-
include
|
72
|
+
include Dependency::Logger
|
25
73
|
|
26
74
|
def some_method_needing_logging
|
27
75
|
logger.info 'some info'
|
76
|
+
# logger blocks are also supported
|
77
|
+
logger.invented do
|
78
|
+
find_answer_to_ultimate_question_of life, universe and ObjectSpace.each_object
|
79
|
+
'using the invented log level is computationally expensive so ' \
|
80
|
+
'I pass this computation as a block to avoid computing it in ' \
|
81
|
+
'case the logger filters this level'
|
82
|
+
end
|
28
83
|
end
|
29
84
|
end
|
85
|
+
```
|
30
86
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
87
|
+
If you write this inside `Dependency::Logger`, you will get a `mylogger` method in
|
88
|
+
`Dependency::Logger` requiring the `:debug` and `:info` levels and falling back
|
89
|
+
to the Ruby logger, identified for injection with `Dependency::Logger`:
|
90
|
+
```ruby
|
91
|
+
InjectedLogger.use :debug, :info, method_name: :mylogger do
|
92
|
+
require 'logger'
|
93
|
+
{ logger: Logger.new(STDERR)
|
37
94
|
end
|
38
95
|
```
|
39
96
|
|
40
|
-
|
41
|
-
|
97
|
+
You can also force the definition of the method elsewhere:
|
42
98
|
```ruby
|
43
|
-
InjectedLogger.
|
99
|
+
InjectedLogger.use :debug, :info, method_name: :mylogger, on: Dependency::OtherLogger do
|
100
|
+
[...]
|
101
|
+
end
|
44
102
|
```
|
45
103
|
|
46
|
-
|
47
|
-
|
104
|
+
If you omit the block, InjectedLogger will provide a default logger (note that
|
105
|
+
we are forced to use the `:on` parameter if we have no block).
|
48
106
|
```ruby
|
49
|
-
InjectedLogger.
|
107
|
+
InjectedLogger.use :debug, :info, on: self
|
50
108
|
```
|
51
109
|
|
110
|
+
Note that you do not need to specify the `:on` parameter for `InjectedLogger.use`
|
111
|
+
nor `InjectedLogger.after_injection` IFF you provide a block to those methods
|
112
|
+
and want to imply `on: self`.
|
113
|
+
|
52
114
|
## Generating the gem
|
53
115
|
|
54
116
|
Both bundler and rspec are required to build the gem:
|
@@ -40,7 +40,7 @@ module InjectedLogger
|
|
40
40
|
if logger and logger != logger_obj
|
41
41
|
raise InUse, "#{self} was already using logger #{logger}"
|
42
42
|
end
|
43
|
-
inject! logger_obj, levels: levels, fallback: fallback
|
43
|
+
inject! logger_obj, levels: levels, fallback: fallback, prefix: prefix
|
44
44
|
end
|
45
45
|
|
46
46
|
def inject!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
|
data/lib/injectedlogger.rb
CHANGED
@@ -33,16 +33,18 @@ module InjectedLogger
|
|
33
33
|
# logger method is called, so that it does not 'require' anything if it is not
|
34
34
|
# needed. :)
|
35
35
|
|
36
|
-
def self.use(*required, on: nil, method_name: :logger, &blk)
|
36
|
+
def self.use(*required, on: nil, as: nil, method_name: :logger, &blk)
|
37
37
|
if on.nil?
|
38
38
|
raise InjectedLogger::DefaultInjectionBlockMissing if blk.nil?
|
39
39
|
on = blk.binding.eval 'self'
|
40
40
|
else
|
41
41
|
on = on.singleton_class unless on.is_a? Module
|
42
42
|
end
|
43
|
-
|
43
|
+
target = on
|
44
|
+
on = as unless as.nil?
|
45
|
+
target.send :define_method, method_name do
|
44
46
|
# avoid recursion if someone calls logger in the block
|
45
|
-
|
47
|
+
target.send :remove_method, method_name
|
46
48
|
unless InjectedLogger.injected? on: on
|
47
49
|
args = blk ? blk.call : nil
|
48
50
|
InjectedLogger.inject_logger args, required, on: on
|
@@ -55,7 +57,7 @@ module InjectedLogger
|
|
55
57
|
required -= thislogger.level_info[:supported]
|
56
58
|
raise InjectedLogger::UnsupportedLevels.new(required) unless required.empty?
|
57
59
|
end
|
58
|
-
|
60
|
+
target.send :define_method, method_name do
|
59
61
|
thislogger
|
60
62
|
end
|
61
63
|
thislogger.after_hook.call(thislogger) if thislogger.after_hook
|
@@ -71,7 +73,7 @@ module InjectedLogger
|
|
71
73
|
{ logger: ::Logger.new(STDERR) }
|
72
74
|
end
|
73
75
|
|
74
|
-
def self.inject_logger(args, required,
|
76
|
+
def self.inject_logger(args, required, **options)
|
75
77
|
args ||= {}
|
76
78
|
args = default_logger.merge(args) unless args.has_key? :logger
|
77
79
|
logger = args.delete :logger
|
@@ -80,7 +82,7 @@ module InjectedLogger
|
|
80
82
|
args[:levels].push(required).flatten!
|
81
83
|
args[:levels].uniq!
|
82
84
|
end
|
83
|
-
args
|
85
|
+
args.merge!(**options)
|
84
86
|
InjectedLogger.inject(logger, **args)
|
85
87
|
end
|
86
88
|
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.10
|
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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|