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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5dd3d22d80668b023317badf017352dd0f0abf70
4
- data.tar.gz: d31bae1ac80c58e7bd389d7337c8fe0f16932cf6
3
+ metadata.gz: b509e96629351ab9e5dad7429cff1942e09a12a6
4
+ data.tar.gz: a54f79bdf60b07015830f44749b8de05127a47e9
5
5
  SHA512:
6
- metadata.gz: a32adcb1e875e2d82d57e4bc906f09e0f565e5233009e6e320b99a0b42d7e88f538584b77e20f00ac8b8107a529c31f47b347b36d70ae3761977e7b79d4bb1ac
7
- data.tar.gz: 2e22f22c43ef4ad755edd660cbd4be03fe8326bf2fa7dd56fd5b9c4b5b67899fd4470bd143df46a52b15e017972b3b3ee23a5c118820f79f852c7041781c82dd
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 a logger in your Ruby code.
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
- def self.injected?
6
- Logger.injected?
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
- Logger.inject(*args)
19
+ def self.inject!(*args, on:, **options)
20
+ logger[on].inject!(*args, **options)
11
21
  end
12
22
 
13
- def self.inject!(*args)
14
- Logger.inject!(*args)
23
+ def self.after_injection(&blk)
24
+ on = blk.binding.eval 'self'
25
+ logger[on].after_hook = blk
15
26
  end
16
27
 
17
- module Logger
28
+ class Logger
18
29
  UNKNOWN = :unknown
19
30
  LOGLEVELS = [:debug, :verbose, :notice, :info, :warn, :error, :critical, :fatal, :unknown]
20
31
 
21
- class << self
22
- attr_reader :prefix, :levels, :level_info, :fallback
32
+ attr_reader :prefix, :levels, :level_info, :fallback
33
+ attr_accessor :after_hook
23
34
 
24
- def injected?
25
- not logger.nil?
26
- end
35
+ def injected?
36
+ not logger.nil?
37
+ end
27
38
 
28
- def inject(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
29
- if logger and logger != logger_obj
30
- raise InUse, "#{self} was already using logger #{logger}"
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
- def inject!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
36
- self.logger = logger_obj
37
- set_prefix prefix
38
- set_levels levels
39
- set_fallback fallback
40
- add_methods
41
- self
42
- end
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
- def prefix=(prefix)
45
- set_prefix prefix
46
- add_methods
47
- end
55
+ def prefix=(prefix)
56
+ set_prefix prefix
57
+ add_methods
58
+ end
48
59
 
49
- def levels=(levels)
50
- set_levels(levels)
51
- add_methods
52
- end
60
+ def levels=(levels)
61
+ set_levels(levels)
62
+ add_methods
63
+ end
53
64
 
54
- def add_levels(*levels)
55
- lvls = self.levels + levels
56
- lvls.uniq!
57
- self.levels = lvls
58
- end
65
+ def add_levels(*levels)
66
+ lvls = self.levels + levels
67
+ lvls.uniq!
68
+ self.levels = lvls
69
+ end
59
70
 
60
- def fallback=(level)
61
- set_fallback level
62
- add_methods
63
- end
71
+ def fallback=(level)
72
+ set_fallback level
73
+ add_methods
74
+ end
64
75
 
65
- def method_missing(method, *args, &blk)
66
- logger.send method, *args, &blk
67
- rescue NoMethodError
68
- super
69
- end
76
+ def method_missing(method, *args, &blk)
77
+ logger.send method, *args, &blk
78
+ rescue NoMethodError
79
+ super
80
+ end
70
81
 
71
- def respond_to_missing?(method, priv = false)
72
- logger.respond_to?(method, priv) || super
73
- end
82
+ def respond_to_missing?(method, priv = false)
83
+ logger.respond_to?(method, priv) || super
84
+ end
74
85
 
75
- private
86
+ private
76
87
 
77
- attr_accessor :logger
78
- attr_writer :level_info
88
+ attr_accessor :logger
89
+ attr_writer :level_info
79
90
 
80
- def set_prefix(prefix)
81
- @prefix = prefix
82
- end
91
+ def set_prefix(prefix)
92
+ @prefix = prefix
93
+ end
83
94
 
84
- def set_levels(levels)
85
- @levels = levels
86
- end
95
+ def set_levels(levels)
96
+ @levels = levels
97
+ end
87
98
 
88
- def set_fallback(level)
89
- @fallback = level
90
- end
99
+ def set_fallback(level)
100
+ @fallback = level
101
+ end
91
102
 
92
- def add_methods
93
- old_levels = level_info ? level_info[:supported] : []
94
- self.level_info = InjectedLogger::Delegator.delegate_levels(
95
- from: logger, on: self, prefix: prefix, extra_levels: self.levels,
96
- old_levels: old_levels, fallback: fallback)
97
- set_levels(level_info[:supported]).tap do
98
- info_message(level_info) if level_info[:info]
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
- def info_message(level_info)
103
- message = if level_info[:fallback]
104
- "non-native log levels #{level_info[:nonnative].join ', '} emulated" \
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
@@ -1,3 +1,3 @@
1
1
  module InjectedLogger
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -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 -= InjectedLogger::Logger.level_info[:supported]
52
+ required -= thislogger.level_info[:supported]
52
53
  unless required.empty?
53
- InjectedLogger::Logger.add_levels(*required)
54
- required -= InjectedLogger::Logger.level_info[:supported]
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
- InjectedLogger::Logger
59
+ thislogger
59
60
  end
60
- InjectedLogger.after_hook.call(InjectedLogger::Logger) if InjectedLogger.after_hook
61
- InjectedLogger::Logger
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
- InjectedLogger.inject(logger, args)
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.7
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-09 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler