injectedlogger 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c1110f07cfbbe6a10e93eed0e971351cb165f4b
4
- data.tar.gz: f8c373e8b28fc600b561e52c8ebf676a7b3c3471
3
+ metadata.gz: 7c397eed4509dee6f22849d012f405627ec44d0b
4
+ data.tar.gz: ffb8aef686ebfb15a7d94586d254b0ebe31ed50e
5
5
  SHA512:
6
- metadata.gz: 8d7d74723b918d031678ab8ff90194e41b8bddcd72393037877aed3318802b5b2d64aae7ae27cd68259a71af30430c29072283b879f7c1cd98eb417c0dc10985
7
- data.tar.gz: 7d9734789ff5eae1f754bdb43466dedbc6ff092beed42ac0571531eccd995f7bfe284fec33f8cc7d21013e1df13ab4b72bd7be8a4ab1d4b7b717b2d6888d8eb7
6
+ metadata.gz: 9d042ff0938ab25cdec8af039da44d9ef276f2a6b7458f80c573ec5d892adf769cf081d8422d34dd4def747dd9b7aa7c038678d417568e81a654c60abd6e3feb
7
+ data.tar.gz: 00655caa8274a8713e7adb4de3672da75c90958c435e5ffb119fe8d59e4f2d2507f3f6aba656b63b73457c0bb73d4ec51ac9409505469ba099f5417378f829e0
data/README.md CHANGED
@@ -17,9 +17,13 @@ or you can set-up a default injection for your logger in case no one else sets i
17
17
  ```ruby
18
18
  module MyLogger
19
19
  InjectedLogger.declare required: [:debug, :info] do
20
+ # this gets executed if no logger has been injected at use time
20
21
  require 'logger'
21
22
  { logger: Logger.new(STDERR), prefix: '[mylogger]' }
22
23
  end
24
+ InjectedLogger.after_injection do |logger|
25
+ logger.info 'ok' # you can also force your prefix with logger.prefix = str
26
+ end
23
27
  end
24
28
 
25
29
  class WantsLogging
@@ -32,7 +36,10 @@ end
32
36
 
33
37
  class ThisAlsoWantsIt
34
38
  include MyLogger
35
- ...
39
+
40
+ def some_other_method_with_debug_logging
41
+ logger.debug 'some_debug_info'
42
+ end
36
43
  end
37
44
  ```
38
45
 
@@ -62,8 +62,15 @@ module InjectedLogger
62
62
  def add_as_fallback(nonnative, fallback)
63
63
  nonnative.each do |lvl|
64
64
  remove_level lvl
65
- add_level lvl do |msg|
66
- public_send fallback, "[#{lvl.upcase}] #{msg}"
65
+ add_level lvl do |*msg, &blk|
66
+ if blk
67
+ public_send fallback, *msg do
68
+ str = blk.call
69
+ "[#{lvl.upcase}] #{str}"
70
+ end
71
+ else
72
+ public_send fallback, "[#{lvl.upcase}] #{msg.join ' '}"
73
+ end
67
74
  end
68
75
  end
69
76
  end
@@ -92,12 +99,19 @@ module InjectedLogger
92
99
 
93
100
  def add_level_method(lvl)
94
101
  if prefix.nil? or prefix.empty?
95
- add_level lvl do |msg|
96
- logger.send lvl, msg
102
+ add_level lvl do |*msg, &blk|
103
+ logger.send lvl, *msg, &blk
97
104
  end
98
105
  else
99
- add_level lvl do |msg|
100
- logger.send lvl, "#{prefix} #{msg}"
106
+ add_level lvl do |*msg, &blk|
107
+ if blk
108
+ logger.send lvl, *msg do
109
+ str = blk.call
110
+ "#{prefix} #{str}"
111
+ end
112
+ else
113
+ logger.send lvl, "#{prefix} #{msg.join ' '}"
114
+ end
101
115
  end
102
116
  end
103
117
  end
@@ -112,14 +126,25 @@ module InjectedLogger
112
126
  if prefix and not prefix.empty?
113
127
  prefix_s += " " + prefix
114
128
  end
115
- klass.define_singleton_method lvl do |msg|
116
- logger.send :log, "#{prefix_s} #{msg}"
129
+ klass.define_singleton_method lvl do |*msg, &blk|
130
+ if blk
131
+ logger.send :log, *msg do
132
+ str = blk.call
133
+ "#{prefix_s} #{str}"
134
+ end
135
+ else
136
+ logger.send :log, "#{prefix_s} #{msg.join ' '}"
137
+ end
117
138
  end
118
139
  else
119
140
  # assume two or more params, best effort with 1st being level
120
141
  if lvl_s = ruby_logger_severity(lvl)
121
- klass.define_singleton_method lvl do |msg|
122
- logger.send :log, lvl_s, msg
142
+ klass.define_singleton_method lvl do |*msg, &blk|
143
+ if blk
144
+ logger.send :log, lvl_s, *msg, &blk
145
+ else
146
+ logger.send :log, lvl_s, msg.join(' ')
147
+ end
123
148
  end
124
149
  end
125
150
  end
@@ -1,3 +1,3 @@
1
1
  module InjectedLogger
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -8,7 +8,8 @@ module InjectedLogger
8
8
  # module MyLogger
9
9
  # InjectedLogger.inject do
10
10
  # require 'logger'
11
- # # logger is required, the rest are other inject() params
11
+ # # parameters are inject() params, none is required, but if
12
+ # # logger is not present, a default one will be used.
12
13
  # { logger: Logger.new(STDERR), prefix: '[mylogger]', ... }
13
14
  # end
14
15
  # end
@@ -38,7 +39,6 @@ module InjectedLogger
38
39
  on.send :remove_method, method_name
39
40
  unless InjectedLogger::Logger.injected?
40
41
  args = blk ? blk.call : nil
41
- args = InjectedLogger.default_logger if args.nil? or args == :default
42
42
  InjectedLogger.inject_logger args, required
43
43
  end
44
44
  required.uniq!
@@ -47,10 +47,20 @@ module InjectedLogger
47
47
  on.send :define_method, method_name do
48
48
  InjectedLogger::Logger
49
49
  end
50
+ InjectedLogger.after_hook.call(InjectedLogger::Logger) if InjectedLogger.after_hook
50
51
  InjectedLogger::Logger
51
52
  end
52
53
  end
53
54
 
55
+ def self.after_injection(&blk)
56
+ self.after_hook = blk
57
+ end
58
+
59
+ class << self
60
+ attr_accessor :after_hook
61
+ private :after_hook=
62
+ end
63
+
54
64
  private
55
65
 
56
66
  def self.default_logger
@@ -59,10 +69,13 @@ module InjectedLogger
59
69
  end
60
70
 
61
71
  def self.inject_logger(args, required)
72
+ args ||= {}
73
+ args = default_logger.merge(args) unless args.has_key? :logger
62
74
  logger = args.delete :logger
63
75
  unless required.empty?
64
76
  args[:levels] ||= []
65
- args[:levels].push(required).flatten!.uniq!
77
+ args[:levels].push(required).flatten!
78
+ args[:levels].uniq!
66
79
  end
67
80
  InjectedLogger::Logger.inject(logger, args)
68
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: injectedlogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Martinez Ruiz