smart_logger_wrapper 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -6
- data/lib/smart_logger_wrapper/version.rb +1 -1
- data/lib/smart_logger_wrapper.rb +16 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c8ddd160c2ce65d59bc832aca8b0aa4c16294be
|
4
|
+
data.tar.gz: 1943139dbae45139e9bcc690cf095f45ed252625
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea9d1354d530b8a45e82b0421225a13a3ba7a794026c3072b914e7f094913285e29160e68a94521c742e05c6598db461b962a45a9d377a6b95c891de53aa089d
|
7
|
+
data.tar.gz: 29256c2b98a5826d298abb908dba83c37d017dcc854ee3c934c3e1b18e106e8f8128ec98c5b1a47f459462cb92c21d06611fd6fe10182fbeaddd7d7c065daf52
|
data/README.md
CHANGED
@@ -31,12 +31,13 @@ Wrap your logger with `SmartLoggerWrapper`, for example, in `config/environments
|
|
31
31
|
|
32
32
|
Note that it is strongly recommended to use the wrapper for all environments so that you can avoid exceptions such as `NoMethodError` due to the unique features of this library.
|
33
33
|
|
34
|
-
You may want to put log messages to STDOUT in
|
34
|
+
You may want to put log messages to `STDOUT` in your development environment. Then:
|
35
35
|
|
36
|
-
```
|
37
|
-
logger =
|
38
|
-
|
39
|
-
|
36
|
+
```ruby
|
37
|
+
config.logger = SmartLoggerWrapper.new(
|
38
|
+
SmartLoggerWrapper.new(Logger.new("log/development.log")).with_position,
|
39
|
+
ActiveSupport::Logger.new(STDOUT)
|
40
|
+
)
|
40
41
|
```
|
41
42
|
|
42
43
|
## Usage
|
@@ -52,9 +53,12 @@ require 'smart_logger_wrapper'
|
|
52
53
|
logger = SmartLoggerWrapper.new(Logger.new('log/development.log'))
|
53
54
|
|
54
55
|
logger.info 'Call logging methods as usual.'
|
56
|
+
|
57
|
+
# You can wrap multiple loggers
|
58
|
+
logger2 = SmartLoggerWrapper.new(Logger.new('log/development.log'), Logger.new(STDOUT))
|
55
59
|
```
|
56
60
|
|
57
|
-
The compatibles must respond to `#
|
61
|
+
The compatibles must respond to `#add` with the same arguments as `Logger#add`.
|
58
62
|
|
59
63
|
### Feature 1: Integrate multiple logger calls
|
60
64
|
|
data/lib/smart_logger_wrapper.rb
CHANGED
@@ -14,10 +14,10 @@ class SmartLoggerWrapper
|
|
14
14
|
unknown: UNKNOWN
|
15
15
|
}.freeze
|
16
16
|
|
17
|
-
attr_reader :
|
17
|
+
attr_reader :loggers, :options
|
18
18
|
|
19
|
-
def initialize(logger = Logger.new(STDOUT), **options)
|
20
|
-
@
|
19
|
+
def initialize(logger = Logger.new(STDOUT), *loggers, **options)
|
20
|
+
@loggers = [logger, *loggers]
|
21
21
|
@options = options
|
22
22
|
end
|
23
23
|
|
@@ -42,14 +42,18 @@ class SmartLoggerWrapper
|
|
42
42
|
Options.apply_all!(messages, options)
|
43
43
|
true
|
44
44
|
rescue Options::ApplicationError => e
|
45
|
-
logger
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
loggers.each do |logger|
|
46
|
+
logger.error(<<~EOM)
|
47
|
+
Failed to apply options: #{e.inspect}
|
48
|
+
#{e.backtrace.join("\n")}
|
49
|
+
EOM
|
50
|
+
end
|
49
51
|
false
|
50
52
|
end.tap do |succeeded|
|
51
53
|
messages.each do |message|
|
52
|
-
|
54
|
+
loggers.each do |logger|
|
55
|
+
logger.add(severity, nil, message)
|
56
|
+
end
|
53
57
|
end
|
54
58
|
end
|
55
59
|
end
|
@@ -70,12 +74,14 @@ class SmartLoggerWrapper
|
|
70
74
|
def method_missing(method_name, *args, &block)
|
71
75
|
if Options.defined_option?(method_name)
|
72
76
|
# if there is an defined option with the same name as the method name, return a new logger with the option.
|
73
|
-
new_logger = self.class.new(
|
77
|
+
new_logger = self.class.new(*loggers, **options.merge(method_name => args.first))
|
74
78
|
return block.(new_logger) if block_given?
|
75
79
|
new_logger
|
76
80
|
else
|
77
81
|
# otherwise, call the method of the warpped logger.
|
78
|
-
|
82
|
+
loggers.each do |logger|
|
83
|
+
logger.public_send(method_name, *args, &block)
|
84
|
+
end
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|