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 +4 -4
- data/README.md +24 -0
- data/lib/injectedlogger/errors.rb +10 -0
- data/lib/injectedlogger/logger.rb +10 -7
- data/lib/injectedlogger/version.rb +1 -1
- data/lib/injectedlogger.rb +50 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76e0c6ca83edad8369e89640d915741e7c0243c8
|
4
|
+
data.tar.gz: ca24a98f8a4ad30d4d3982b641b3ac7b589e4bb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
@@ -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, :
|
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
|
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
|
data/lib/injectedlogger.rb
CHANGED
@@ -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.
|
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
|