injectedlogger 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/injectedlogger.rb +30 -12
- data/lib/injectedlogger/errors.rb +1 -0
- data/lib/injectedlogger/logger.rb +4 -4
- data/lib/injectedlogger/version.rb +1 -1
- data/spec/logger_spec.rb +1 -1
- 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: 6c1110f07cfbbe6a10e93eed0e971351cb165f4b
|
4
|
+
data.tar.gz: f8c373e8b28fc600b561e52c8ebf676a7b3c3471
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d7d74723b918d031678ab8ff90194e41b8bddcd72393037877aed3318802b5b2d64aae7ae27cd68259a71af30430c29072283b879f7c1cd98eb417c0dc10985
|
7
|
+
data.tar.gz: 7d9734789ff5eae1f754bdb43466dedbc6ff092beed42ac0571531eccd995f7bfe284fec33f8cc7d21013e1df13ab4b72bd7be8a4ab1d4b7b717b2d6888d8eb7
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ It will try to support as many methods and levels as the underlying object suppo
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
logger = InjectedLogger::Logger.
|
10
|
+
logger = InjectedLogger::Logger.inject somelogger
|
11
11
|
raise 'No info :(' unless logger.level_info[:supported].include? :info
|
12
12
|
logger.info 'You now have a logger!'
|
13
13
|
```
|
@@ -16,7 +16,7 @@ or you can set-up a default injection for your logger in case no one else sets i
|
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
module MyLogger
|
19
|
-
InjectedLogger.
|
19
|
+
InjectedLogger.declare required: [:debug, :info] do
|
20
20
|
require 'logger'
|
21
21
|
{ logger: Logger.new(STDERR), prefix: '[mylogger]' }
|
22
22
|
end
|
data/lib/injectedlogger.rb
CHANGED
@@ -8,7 +8,7 @@ module InjectedLogger
|
|
8
8
|
# module MyLogger
|
9
9
|
# InjectedLogger.inject do
|
10
10
|
# require 'logger'
|
11
|
-
# # logger is required, the rest are other
|
11
|
+
# # logger is required, the rest are other inject() params
|
12
12
|
# { logger: Logger.new(STDERR), prefix: '[mylogger]', ... }
|
13
13
|
# end
|
14
14
|
# end
|
@@ -26,18 +26,20 @@ module InjectedLogger
|
|
26
26
|
# logger method is called, so that it does not 'require' anything if it is not
|
27
27
|
# needed. :)
|
28
28
|
|
29
|
-
def self.
|
30
|
-
on
|
31
|
-
|
29
|
+
def self.declare(on: nil, required: [], method_name: :logger, &blk)
|
30
|
+
if on.nil?
|
31
|
+
raise InjectedLogger::DefaultInjectionBlockMissing if blk.nil?
|
32
|
+
on = blk.binding.eval 'self'
|
33
|
+
else
|
34
|
+
on = on.singleton_class unless on.is_a? Module
|
35
|
+
end
|
32
36
|
on.send :define_method, method_name do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
InjectedLogger::Logger.use(logger, args)
|
37
|
+
# avoid recursion if someone calls logger in the block
|
38
|
+
on.send :remove_method, method_name
|
39
|
+
unless InjectedLogger::Logger.injected?
|
40
|
+
args = blk ? blk.call : nil
|
41
|
+
args = InjectedLogger.default_logger if args.nil? or args == :default
|
42
|
+
InjectedLogger.inject_logger args, required
|
41
43
|
end
|
42
44
|
required.uniq!
|
43
45
|
required -= InjectedLogger::Logger.level_info[:supported]
|
@@ -48,4 +50,20 @@ module InjectedLogger
|
|
48
50
|
InjectedLogger::Logger
|
49
51
|
end
|
50
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def self.default_logger
|
57
|
+
require 'logger'
|
58
|
+
{ logger: ::Logger.new(STDERR) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.inject_logger(args, required)
|
62
|
+
logger = args.delete :logger
|
63
|
+
unless required.empty?
|
64
|
+
args[:levels] ||= []
|
65
|
+
args[:levels].push(required).flatten!.uniq!
|
66
|
+
end
|
67
|
+
InjectedLogger::Logger.inject(logger, args)
|
68
|
+
end
|
51
69
|
end
|
@@ -9,18 +9,18 @@ module InjectedLogger
|
|
9
9
|
class << self
|
10
10
|
attr_reader :prefix, :levels, :level_info, :fallback
|
11
11
|
|
12
|
-
def
|
12
|
+
def injected?
|
13
13
|
not logger.nil?
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def inject(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
|
17
17
|
if logger and logger != logger_obj
|
18
18
|
raise InUse, "#{self} was already using logger #{logger}"
|
19
19
|
end
|
20
|
-
|
20
|
+
inject! logger_obj, levels: levels, fallback: fallback
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
23
|
+
def inject!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
|
24
24
|
self.logger = logger_obj
|
25
25
|
set_prefix prefix
|
26
26
|
set_levels levels
|
data/spec/logger_spec.rb
CHANGED
@@ -66,7 +66,7 @@ module InjectedLogger
|
|
66
66
|
let(:logger_object) { Helpers::RubyLikeLogger.new [] }
|
67
67
|
|
68
68
|
before do
|
69
|
-
InjectedLogger::Logger.
|
69
|
+
InjectedLogger::Logger.inject! logger_object
|
70
70
|
#l = InjectedLogger::Logger
|
71
71
|
#STDERR.puts "Native: #{l.level_info[:native]} Non-native: #{l.level_info[:nonnative]} Fallback: #{l.level_info[:fallback]} Info: #{l.level_info[:info]}"
|
72
72
|
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.5
|
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-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|