extended_logger 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/extended_logger.rb +2 -0
- data/lib/extended_logger/controls.rb +35 -25
- data/lib/extended_logger/define.rb +23 -5
- data/lib/extended_logger/extended_logger.rb +26 -3
- data/lib/extended_logger/level/set.rb +4 -4
- data/lib/extended_logger/logger.rb +3 -0
- data/lib/extended_logger/registry.rb +107 -0
- data/lib/extended_logger/serializer/text_formatter.rb +2 -1
- data/lib/extended_logger/sink.rb +24 -3
- data/lib/extended_logger/verbosity_threshold.rb +4 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4eb522f68f7697e65cffd4539df8219879be9973
|
4
|
+
data.tar.gz: 0eda1536929c52214abe517846b7913adfe372da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8150607148ff582e2e8f959f67d614a05551bbc87bc227a272950a201a7639a2f12a70d7e118e5dfb4de215bfe2a18e5f6175e14987502edac6442ce0f508109
|
7
|
+
data.tar.gz: 95d3dae30748c24e11ddfc5813e4c818514535a8318268c813d09a1b4bfec3016a1192a9e102ffe1e8dcd88c0e81f60f8cacec2cb0b1554e23146281c7f7d727
|
data/lib/extended_logger.rb
CHANGED
@@ -7,7 +7,9 @@ require_relative './extended_logger/define'
|
|
7
7
|
require_relative './extended_logger/extended_logger'
|
8
8
|
require_relative './extended_logger/level'
|
9
9
|
require_relative './extended_logger/level/set'
|
10
|
+
require_relative './extended_logger/logger'
|
10
11
|
require_relative './extended_logger/message'
|
12
|
+
require_relative './extended_logger/registry'
|
11
13
|
require_relative './extended_logger/rule'
|
12
14
|
require_relative './extended_logger/serializer'
|
13
15
|
require_relative './extended_logger/serializer/text_formatter'
|
@@ -11,7 +11,7 @@ class ExtendedLogger
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module None
|
14
|
-
def self.
|
14
|
+
def self.example
|
15
15
|
ColorScheme::None
|
16
16
|
end
|
17
17
|
end
|
@@ -19,7 +19,7 @@ class ExtendedLogger
|
|
19
19
|
|
20
20
|
module Formatters
|
21
21
|
module Plain
|
22
|
-
def self.
|
22
|
+
def self.example
|
23
23
|
-> message do
|
24
24
|
"#{message.level} -- #{message.prose}"
|
25
25
|
end
|
@@ -28,7 +28,7 @@ class ExtendedLogger
|
|
28
28
|
end
|
29
29
|
|
30
30
|
module Label
|
31
|
-
def self.
|
31
|
+
def self.example
|
32
32
|
'SomeClass'
|
33
33
|
end
|
34
34
|
end
|
@@ -38,7 +38,7 @@ class ExtendedLogger
|
|
38
38
|
Set.ruby_logger.get :info
|
39
39
|
end
|
40
40
|
|
41
|
-
def self.
|
41
|
+
def self.example label=nil
|
42
42
|
label ||= :normal
|
43
43
|
Set.get label
|
44
44
|
end
|
@@ -48,7 +48,7 @@ class ExtendedLogger
|
|
48
48
|
%i(debug info warn error fatal)
|
49
49
|
end
|
50
50
|
|
51
|
-
def self.
|
51
|
+
def self.example
|
52
52
|
%i(verbose normal)
|
53
53
|
end
|
54
54
|
end
|
@@ -64,26 +64,36 @@ class ExtendedLogger
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def self.example
|
67
|
-
names = Names.
|
67
|
+
names = Names.example
|
68
68
|
Level::Set.build names, :default => :normal
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
class Logger < ExtendedLogger.define
|
74
|
-
def self.example
|
75
|
-
device
|
73
|
+
class Logger < ExtendedLogger.define Levels::Set.example, :format => Formatters::Plain.example
|
74
|
+
def self.example
|
75
|
+
device = StringIO.new
|
76
76
|
|
77
77
|
instance = build device
|
78
78
|
instance.clock = Time::Clock
|
79
79
|
instance
|
80
80
|
end
|
81
|
+
|
82
|
+
class Ruby < ExtendedLogger.define
|
83
|
+
def self.example
|
84
|
+
device = StringIO.new
|
85
|
+
|
86
|
+
instance = build device
|
87
|
+
instance.clock = Time::Clock
|
88
|
+
instance
|
89
|
+
end
|
90
|
+
end
|
81
91
|
end
|
82
92
|
|
83
93
|
module Messages
|
84
94
|
def self.example prose: nil, level: nil, label: nil, time: nil
|
85
95
|
prose ||= Prose.example
|
86
|
-
level ||= Levels.
|
96
|
+
level ||= Levels.example
|
87
97
|
time ||= Time.reference
|
88
98
|
|
89
99
|
if prose.is_a? Proc
|
@@ -96,15 +106,15 @@ class ExtendedLogger
|
|
96
106
|
end
|
97
107
|
|
98
108
|
module Label
|
99
|
-
def self.
|
100
|
-
label ||= Control::Label.
|
109
|
+
def self.example label=nil
|
110
|
+
label ||= Control::Label.example
|
101
111
|
|
102
112
|
Messages.example label: label
|
103
113
|
end
|
104
114
|
end
|
105
115
|
|
106
116
|
module RubyLogger
|
107
|
-
def self.
|
117
|
+
def self.example label=nil
|
108
118
|
level = Level.new :info
|
109
119
|
|
110
120
|
Messages.example level: level, label: label
|
@@ -113,22 +123,22 @@ class ExtendedLogger
|
|
113
123
|
end
|
114
124
|
|
115
125
|
module Output
|
116
|
-
def self.
|
126
|
+
def self.example prose=nil
|
117
127
|
prose ||= Prose.example
|
118
128
|
"NORMAL -- #{prose}\n"
|
119
129
|
end
|
120
130
|
|
121
131
|
module Colorized
|
122
|
-
def self.
|
132
|
+
def self.example prose=nil
|
123
133
|
prose ||= Prose.example
|
124
134
|
colorized_prose = "\e[1;37;40m#{prose}\e[0m"
|
125
135
|
|
126
|
-
Output.
|
136
|
+
Output.example colorized_prose
|
127
137
|
end
|
128
138
|
end
|
129
139
|
|
130
140
|
module MultipleLines
|
131
|
-
def self.
|
141
|
+
def self.example
|
132
142
|
"NORMAL -- Multiple\nNORMAL -- line\nNORMAL -- message\n"
|
133
143
|
end
|
134
144
|
end
|
@@ -138,7 +148,7 @@ class ExtendedLogger
|
|
138
148
|
@log_format ||= ::Logger::Formatter.new
|
139
149
|
end
|
140
150
|
|
141
|
-
def self.
|
151
|
+
def self.example progname=nil
|
142
152
|
severity = 'INFO'
|
143
153
|
time = Time::Clock.now
|
144
154
|
message = Prose.example
|
@@ -148,34 +158,34 @@ class ExtendedLogger
|
|
148
158
|
end
|
149
159
|
|
150
160
|
module RuleApplied
|
151
|
-
def self.
|
161
|
+
def self.example
|
152
162
|
prose = Prose.example
|
153
163
|
prose = prose.upcase
|
154
164
|
|
155
|
-
Output.
|
165
|
+
Output.example prose
|
156
166
|
end
|
157
167
|
end
|
158
168
|
end
|
159
169
|
|
160
170
|
module Prose
|
161
171
|
def self.example
|
162
|
-
SingleLine.
|
172
|
+
SingleLine.example
|
163
173
|
end
|
164
174
|
|
165
175
|
module EscapeCodes
|
166
|
-
def self.
|
176
|
+
def self.example
|
167
177
|
"\t\r"
|
168
178
|
end
|
169
179
|
end
|
170
180
|
|
171
181
|
module MultipleLines
|
172
|
-
def self.
|
182
|
+
def self.example
|
173
183
|
"Multiple\nline\nmessage"
|
174
184
|
end
|
175
185
|
end
|
176
186
|
|
177
187
|
module SingleLine
|
178
|
-
def self.
|
188
|
+
def self.example
|
179
189
|
"Some log message"
|
180
190
|
end
|
181
191
|
end
|
@@ -194,7 +204,7 @@ class ExtendedLogger
|
|
194
204
|
|
195
205
|
module Sink
|
196
206
|
def self.example device=nil, format: nil, rules: nil
|
197
|
-
format ||= Controls::Formatters::Plain.
|
207
|
+
format ||= Controls::Formatters::Plain.example
|
198
208
|
device ||= StringIO.new
|
199
209
|
levels = Controls::Levels::Set.example
|
200
210
|
|
@@ -1,18 +1,20 @@
|
|
1
1
|
class ExtendedLogger
|
2
|
-
def self.define levels=nil, **
|
2
|
+
def self.define levels=nil, **sink_defaults
|
3
3
|
levels ||= Defaults.levels
|
4
4
|
|
5
5
|
subclass = Class.new self
|
6
|
-
Define.(subclass, levels)
|
6
|
+
Define.(subclass, levels, sink_defaults)
|
7
7
|
subclass
|
8
8
|
end
|
9
9
|
|
10
10
|
class Define
|
11
11
|
attr_reader :levels
|
12
|
+
attr_reader :sink_defaults
|
12
13
|
attr_reader :subclass
|
13
14
|
|
14
|
-
def initialize subclass, levels,
|
15
|
+
def initialize subclass, levels, sink_defaults
|
15
16
|
@levels = levels
|
17
|
+
@sink_defaults = sink_defaults
|
16
18
|
@subclass = subclass
|
17
19
|
end
|
18
20
|
|
@@ -22,8 +24,16 @@ class ExtendedLogger
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def call
|
25
|
-
levels = self.
|
26
|
-
|
27
|
+
levels = self.level_set
|
28
|
+
|
29
|
+
subclass.define_singleton_method :levels do levels end
|
30
|
+
subclass.send :define_method, :levels do self.class.levels end
|
31
|
+
|
32
|
+
if sink_defaults = self.sink_defaults
|
33
|
+
subclass.define_singleton_method :sink_defaults do
|
34
|
+
sink_defaults
|
35
|
+
end
|
36
|
+
end
|
27
37
|
|
28
38
|
levels.each do |level|
|
29
39
|
method_name = level.name
|
@@ -33,5 +43,13 @@ class ExtendedLogger
|
|
33
43
|
end
|
34
44
|
end
|
35
45
|
end
|
46
|
+
|
47
|
+
def level_set
|
48
|
+
if levels.is_a? Level::Set
|
49
|
+
levels
|
50
|
+
else
|
51
|
+
Level::Set.build levels
|
52
|
+
end
|
53
|
+
end
|
36
54
|
end
|
37
55
|
end
|
@@ -8,13 +8,36 @@ class ExtendedLogger
|
|
8
8
|
@sink = sink
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.build
|
12
|
-
sink =
|
11
|
+
def self.build device=nil, **sink_arguments
|
12
|
+
sink = self.sink device: device, **sink_arguments
|
13
13
|
|
14
14
|
instance = new sink
|
15
15
|
instance
|
16
16
|
end
|
17
17
|
|
18
|
+
def self.empty_message
|
19
|
+
'<empty message>'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.levels
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.sink device: nil, **sink_arguments
|
26
|
+
sink_arguments = sink_defaults.merge sink_arguments
|
27
|
+
|
28
|
+
sink_arguments[:levels] ||= levels if levels
|
29
|
+
|
30
|
+
Sink.build device, **sink_arguments
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.sink_defaults
|
34
|
+
{}
|
35
|
+
end
|
36
|
+
|
37
|
+
def << message
|
38
|
+
sink << message
|
39
|
+
end
|
40
|
+
|
18
41
|
def add level, prose=nil, label: nil, &block
|
19
42
|
block ||= ->{ prose }
|
20
43
|
label ||= self.label
|
@@ -23,7 +46,7 @@ class ExtendedLogger
|
|
23
46
|
|
24
47
|
message = Message.new block, level, time, label
|
25
48
|
|
26
|
-
|
49
|
+
self << message
|
27
50
|
end
|
28
51
|
|
29
52
|
def clock
|
@@ -53,10 +53,10 @@ class ExtendedLogger
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def resolve reference
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
56
|
+
case reference
|
57
|
+
when Symbol then get reference
|
58
|
+
when String then get reference.to_sym
|
59
|
+
else get reference.name
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
class ExtendedLogger
|
2
|
+
class Registry < Module
|
3
|
+
attr_reader :attr_name
|
4
|
+
attr_reader :logger_class
|
5
|
+
attr_reader :sink
|
6
|
+
|
7
|
+
def initialize logger_class, sink, attr_name
|
8
|
+
@attr_name = attr_name
|
9
|
+
@logger_class = logger_class
|
10
|
+
@sink = sink
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.build logger_class, attr_name: nil, **sink_arguments
|
14
|
+
attr_name ||= :logger
|
15
|
+
|
16
|
+
sink = logger_class.sink **sink_arguments
|
17
|
+
|
18
|
+
instance = new logger_class, sink, attr_name
|
19
|
+
instance.send :const_set, :Logger, logger_class
|
20
|
+
instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_logger key
|
24
|
+
logger = logger_class.new sink
|
25
|
+
logger.label = key.name
|
26
|
+
logger
|
27
|
+
end
|
28
|
+
|
29
|
+
def cache
|
30
|
+
@cache ||= Hash.new do |cache, key|
|
31
|
+
cache[key] = build_logger key
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear
|
36
|
+
cache.clear
|
37
|
+
end
|
38
|
+
|
39
|
+
def included cls
|
40
|
+
cls.class_exec self do |registry|
|
41
|
+
ivar_name = "@#{registry.attr_name}"
|
42
|
+
cvar_name = "@@#{registry.attr_name}"
|
43
|
+
|
44
|
+
attr_writer registry.attr_name
|
45
|
+
|
46
|
+
define_method registry.attr_name do
|
47
|
+
if instance_variable_defined? ivar_name
|
48
|
+
return instance_variable_get ivar_name
|
49
|
+
end
|
50
|
+
|
51
|
+
if self.is_a? Module
|
52
|
+
target = self
|
53
|
+
else
|
54
|
+
target = self.class
|
55
|
+
end
|
56
|
+
|
57
|
+
if target.class_variable_defined? cvar_name
|
58
|
+
return target.class_variable_get cvar_name
|
59
|
+
end
|
60
|
+
|
61
|
+
logger = registry.get self
|
62
|
+
self.class.class_variable_set cvar_name, logger
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def extended object
|
68
|
+
included object.singleton_class
|
69
|
+
end
|
70
|
+
|
71
|
+
def get receiver=nil
|
72
|
+
receiver ||= Object
|
73
|
+
|
74
|
+
key = self.key receiver
|
75
|
+
|
76
|
+
cache[key]
|
77
|
+
end
|
78
|
+
|
79
|
+
def key receiver
|
80
|
+
if receiver.is_a? Module
|
81
|
+
receiver
|
82
|
+
else
|
83
|
+
receiver.class
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def level
|
88
|
+
sink.level
|
89
|
+
end
|
90
|
+
|
91
|
+
def level= reference
|
92
|
+
sink.level = levels.resolve reference
|
93
|
+
end
|
94
|
+
|
95
|
+
def levels
|
96
|
+
sink.levels
|
97
|
+
end
|
98
|
+
|
99
|
+
def lower_verbosity
|
100
|
+
sink.lower_verbosity
|
101
|
+
end
|
102
|
+
|
103
|
+
def raise_verbosity
|
104
|
+
sink.raise_verbosity
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -13,7 +13,7 @@ class ExtendedLogger
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def call
|
16
|
-
return
|
16
|
+
return [ExtendedLogger.empty_message] if prose.nil? or prose.empty?
|
17
17
|
|
18
18
|
lines.map do |line|
|
19
19
|
line.chomp! if line.match %r{(?<!\r)\n}
|
@@ -24,6 +24,7 @@ class ExtendedLogger
|
|
24
24
|
line = line.inspect
|
25
25
|
line.slice! 0, 1
|
26
26
|
line.slice! -1, 1
|
27
|
+
line.gsub! '\\"', '"'
|
27
28
|
line
|
28
29
|
end
|
29
30
|
end
|
data/lib/extended_logger/sink.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class ExtendedLogger
|
2
2
|
class Sink
|
3
|
-
|
3
|
+
attr_accessor :device
|
4
4
|
attr_reader :levels
|
5
5
|
attr_writer :rules
|
6
6
|
attr_reader :serializer
|
@@ -11,10 +11,15 @@ class ExtendedLogger
|
|
11
11
|
@serializer = serializer
|
12
12
|
end
|
13
13
|
|
14
|
-
def self.build device=nil,
|
14
|
+
def self.build device=nil, color_scheme: nil, format: nil, levels: nil, rules: nil
|
15
15
|
device ||= $stderr
|
16
16
|
levels ||= Defaults.levels
|
17
|
-
|
17
|
+
|
18
|
+
device = File.open device, 'a+' if device.is_a? String
|
19
|
+
device.sync = true
|
20
|
+
|
21
|
+
color_scheme = nil unless device.is_a? StringIO or device.tty?
|
22
|
+
serializer = Serializer.build log_format: format, color_scheme: color_scheme
|
18
23
|
|
19
24
|
instance = new device, levels, serializer
|
20
25
|
instance.rules = rules if rules
|
@@ -37,6 +42,22 @@ class ExtendedLogger
|
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
45
|
+
def level
|
46
|
+
verbosity_threshold.level
|
47
|
+
end
|
48
|
+
|
49
|
+
def level= reference
|
50
|
+
verbosity_threshold.level = levels.resolve reference
|
51
|
+
end
|
52
|
+
|
53
|
+
def lower_verbosity
|
54
|
+
verbosity_threshold.lower
|
55
|
+
end
|
56
|
+
|
57
|
+
def raise_verbosity
|
58
|
+
verbosity_threshold.raise
|
59
|
+
end
|
60
|
+
|
40
61
|
def rules
|
41
62
|
@rules ||= []
|
42
63
|
end
|
@@ -15,14 +15,14 @@ class ExtendedLogger
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def lower
|
18
|
-
if level.
|
19
|
-
self.level = level.
|
18
|
+
if level.next
|
19
|
+
self.level = level.next
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
def raise
|
24
|
-
if level.
|
25
|
-
self.level = level.
|
24
|
+
if level.prev
|
25
|
+
self.level = level.prev
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extended_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Ladd
|
@@ -41,7 +41,9 @@ files:
|
|
41
41
|
- lib/extended_logger/extended_logger.rb
|
42
42
|
- lib/extended_logger/level.rb
|
43
43
|
- lib/extended_logger/level/set.rb
|
44
|
+
- lib/extended_logger/logger.rb
|
44
45
|
- lib/extended_logger/message.rb
|
46
|
+
- lib/extended_logger/registry.rb
|
45
47
|
- lib/extended_logger/rule.rb
|
46
48
|
- lib/extended_logger/serializer.rb
|
47
49
|
- lib/extended_logger/serializer/text_formatter.rb
|