injectedlogger 0.0.2 → 0.0.3

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: 6145235cf44a5de4aa3d8ba7bd99ac587ce3264f
4
- data.tar.gz: 973b5556e555eabb22f035a389fdf8a2c9167610
3
+ metadata.gz: 76e0c6ca83edad8369e89640d915741e7c0243c8
4
+ data.tar.gz: ca24a98f8a4ad30d4d3982b641b3ac7b589e4bb2
5
5
  SHA512:
6
- metadata.gz: 790e2ba277d2019c95f04c742c1cbcd233c76a7ce69c0be77c30aa40af5e227ec2a47c13047501039d15acb49daf66d3bcce20577cf9a1beba5ba20b132d2f49
7
- data.tar.gz: 15f2473bdc1ecb1ba3a3401db0bc21d2f41202985850be832d5e44be516d84a5af8ee8ead2f367d4f22511100427a595c85c67c5be1e5443fb3c063a574d89e6
6
+ metadata.gz: df9f53955854e96a865c6de34db278e401eb24e232d9e5e14c341c32ba7605d0c6ff2074211f6ff71ca1fe0719213be9af96b473c3a086553d4a30348853415e
7
+ data.tar.gz: 1b701f337395fe562fb3c4523402f4d589ab7d504c1a1128c38164af1c44e0d2733f23a959ee0f6460bffd79e13e46e464d30144de85f3e89c3e4fda48cdb0e7
data/README.md CHANGED
@@ -12,6 +12,30 @@ raise 'No info :(' unless logger.level_info[:supported].include? :info
12
12
  logger.info 'You now have a logger!'
13
13
  ```
14
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:
16
+
17
+ ```ruby
18
+ module MyLogger
19
+ InjectedLogger.inject self, required: [:debug, :info] do
20
+ require 'logger'
21
+ { logger: Logger.new(STDERR), prefix: '[mylogger]' }
22
+ end
23
+ end
24
+
25
+ class WantsLogging
26
+ include MyLogger
27
+
28
+ def some_method_needing_logging
29
+ logger.info 'some_info'
30
+ end
31
+ end
32
+
33
+ class ThisAlsoWantsIt
34
+ include MyLogger
35
+ ...
36
+ end
37
+ ```
38
+
15
39
  ## Generating the gem
16
40
 
17
41
  Both bundler and rspec are required to build the gem:
@@ -0,0 +1,10 @@
1
+ module InjectedLogger
2
+ Error = Class.new StandardError
3
+ InUse = Class.new Error
4
+ UnsupportedLevels = Class.new Error do
5
+ def initalize(levels)
6
+ super("logger does not support required levels #{levels.join ', '}")
7
+ end
8
+ end
9
+ end
10
+
@@ -1,12 +1,10 @@
1
+ require 'injectedlogger/errors'
1
2
  require 'injectedlogger/delegator'
2
3
 
3
4
  module InjectedLogger
4
5
  module Logger
5
- Error = Class.new StandardError
6
- InUse = Class.new Error
7
-
8
6
  UNKNOWN = :unknown
9
- LOGLEVELS = [:debug, :verbose, :info, :notice, :warn, :error, :critical, :fatal, :unknown]
7
+ LOGLEVELS = [:debug, :verbose, :notice, :info, :warn, :error, :critical, :fatal, :unknown]
10
8
 
11
9
  class << self
12
10
  attr_reader :prefix, :levels, :level_info, :fallback
@@ -15,19 +13,20 @@ module InjectedLogger
15
13
  not logger.nil?
16
14
  end
17
15
 
18
- def use(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN)
16
+ def use(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
19
17
  if logger and logger != logger_obj
20
18
  raise InUse, "#{self} was already using logger #{logger}"
21
19
  end
22
20
  use! logger_obj, levels: levels, fallback: fallback
23
21
  end
24
22
 
25
- def use!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN)
23
+ def use!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
26
24
  self.logger = logger_obj
27
- set_prefix '[core]'
25
+ set_prefix prefix
28
26
  set_levels levels
29
27
  set_fallback fallback
30
28
  add_methods
29
+ self
31
30
  end
32
31
 
33
32
  def prefix=(prefix)
@@ -49,6 +48,10 @@ module InjectedLogger
49
48
  logger.send method, *args, &blk
50
49
  end
51
50
 
51
+ def respond_to_missing?(method, priv = false)
52
+ logger.respond_to_missing?(method, priv)
53
+ end
54
+
52
55
  private
53
56
 
54
57
  attr_accessor :logger
@@ -1,3 +1,3 @@
1
1
  module InjectedLogger
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -1,2 +1,51 @@
1
+ require 'injectedlogger/errors'
1
2
  require 'injectedlogger/logger'
2
- require 'injectedlogger/version'
3
+ require 'injectedlogger/version'
4
+
5
+ module InjectedLogger
6
+ # inject a default logger in case no one has set one for you:
7
+ #
8
+ # module MyLogger
9
+ # InjectedLogger.inject self do
10
+ # require 'logger'
11
+ # # logger is required, the rest are other use() params
12
+ # { logger: Logger.new(STDERR), prefix: '[mylogger]', ... }
13
+ # end
14
+ # end
15
+ #
16
+ # class WantsLogging
17
+ # include MyLogger
18
+ #
19
+ # def some_method_needing_logging
20
+ # logger.info 'some info'
21
+ # end
22
+ # end
23
+ #
24
+ # This will only run the block passed to inject IFF there was no logger set
25
+ # up to be used by InjectedLogger, and it will only happen the first time the
26
+ # logger method is called, so that it does not 'require' anything if it is not
27
+ # needed. :)
28
+
29
+ def self.inject(klass, required: [], method_name: :logger, &blk)
30
+ klass.send :remove_method, method_name rescue nil
31
+ klass.send :define_method, method_name do
32
+ unless InjectedLogger::Logger.in_use?
33
+ args = blk.call
34
+ logger = args.delete :logger
35
+ unless required.empty?
36
+ args[:levels] ||= []
37
+ args[:levels].push(required).flatten!.uniq!
38
+ end
39
+ InjectedLogger::Logger.use(logger, args)
40
+ end
41
+ required.uniq!
42
+ required -= InjectedLogger::Logger.level_info[:supported]
43
+ raise InjectedLogger::UnsupportedLevels.new(required) if not required.empty?
44
+ klass.send :remove_method, method_name
45
+ klass.send :define_method, method_name do
46
+ InjectedLogger::Logger
47
+ end
48
+ InjectedLogger::Logger
49
+ end
50
+ end
51
+ 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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Martinez Ruiz
@@ -69,6 +69,7 @@ files:
69
69
  - injectedlogger.gemspec
70
70
  - lib/injectedlogger.rb
71
71
  - lib/injectedlogger/delegator.rb
72
+ - lib/injectedlogger/errors.rb
72
73
  - lib/injectedlogger/logger.rb
73
74
  - lib/injectedlogger/version.rb
74
75
  - spec/helpers.rb