-
Notifications
You must be signed in to change notification settings - Fork 98
/
logger.rb
297 lines (277 loc) · 11.5 KB
/
logger.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# coding: utf-8
require 'logger'
require 'delegate'
module RDF; module Util
##
# Helpers for logging errors, warnings and debug information.
#
# Modules must provide `@logger`, which returns an instance of `Logger`, or something responding to `#<<`. Logger may also be specified using an `@options` hash containing a `:logger` entry.
# @since 2.0.0
module Logger
# The IOWrapper class is used to store per-logger state while wrapping an IO such as $stderr.
IOWrapper = DelegateClass(IO)
##
# Logger instance, found using `options[:logger]`, `@logger`, or `@options[:logger]`
# @param [Hash{Symbol => Object}] options
# @option options [Logger, #<<] :logger
# @return [Logger, #write, #<<]
def logger(logger: nil, **options)
# Guard against undefined instance variables, which may be a warning if used.
@options = {} unless instance_variable_defined?(:@options) || frozen?
logger ||= @logger if instance_variable_defined?(:@logger)
logger = @options[:logger] if logger.nil? && instance_variable_defined?(:@options) && @options
if logger.nil?
# Unless otherwise specified, use $stderr
logger = IOWrapper.new($stderr)
# Reset log_statistics so that it's not inherited across different instances
logger.log_statistics.clear if logger.respond_to?(:log_statistics)
end
logger = ::Logger.new(::File.open(::File::NULL, "w")) unless logger # Incase false was used, which is frozen
@options[:logger] ||= logger if instance_variable_defined?(:@options)
logger.extend(LoggerBehavior) unless logger.is_a?(LoggerBehavior)
logger
end
##
# Number of times logger has been called at each level
# @param [Hash{Symbol => Object}] options
# @option options [Logger, #<<] :logger
# @return [Hash{Symbol => Integer}]
def log_statistics(**options)
logger(**options).log_statistics
end
##
# Used for fatal errors where processing cannot continue. If `logger` is not configured, it logs to `$stderr`.
#
# @overload log_fatal(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:fatal)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @option options [Class] :exception, (StandardError)
# Exception class used for raising error
# @yieldreturn [String] added to message
# @return [void]
# @raise Raises the provided exception class using the first element from args as the message component.
def log_fatal(*args, level: :fatal, **options, &block)
logger_common(*args, "Called from #{Gem.location_of_caller.join(':')}", level: level, **options, &block)
raise options.fetch(:exception, StandardError), args.first
end
##
# Used for non-fatal errors where processing can continue. If `logger` is not configured, it logs to `$stderr`.
#
# As a side-effect of setting `@logger_in_error`, which will suppress further error messages until cleared using {#log_recover}.
#
# @overload log_error(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:error)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [:fatal, :error, :warn, :info, :debug] level (:<<)
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @option options [Class] :exception, (StandardError)
# Exception class used for raising error
# @yieldreturn [String] added to message
# @return [void]
# @raise Raises the provided exception class using the first element from args as the message component, if `:exception` option is provided.
def log_error(*args, level: :error, **options, &block)
logger = self.logger(**options)
return if logger.recovering
logger.recovering = true
logger_common(*args, level: level, **options, &block)
raise options[:exception], args.first if options[:exception]
end
# In recovery mode? When `log_error` is called, we enter recovery mode. This is cleared when `log_recover` is called.
# @param [Hash{Symbol => Object}] options
# @option options [Logger, #<<] :logger
# @return [Boolean]
def log_recovering?(**options)
self.logger(**options).recovering
end
##
# Warning message.
#
# @overload log_warn(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:warn)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [:fatal, :error, :warn, :info, :debug] level (:<<)
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @yieldreturn [String] added to message
# @return [void]
def log_warn(*args, level: :warn, **options, &block)
logger_common(*args, level: level, **options, &block)
end
##
# Recovers from an error condition. If `args` are passed, sent as an informational message
#
# As a side-effect of clearing `@logger_in_error`.
#
# @overload log_recover(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:info)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @yieldreturn [String] added to message
# @return [void]
def log_recover(*args, level: :info, **options, &block)
logger = self.logger(**options)
logger.recovering = false
return if args.empty? && !block_given?
logger_common(*args, level: level, **options, &block)
end
##
# Informational message.
#
# @overload log_info(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:info)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @yieldreturn [String] added to message
# @return [void]
def log_info(*args, level: :info, **options, &block)
logger_common(*args, level: level, **options, &block)
end
##
# Debug message.
#
# @overload log_debug(*args, **options, &block)
# @param [Array<String>] args
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level (:debug)
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @yieldreturn [String] added to message
# @return [void]
def log_debug(*args, level: :debug, **options, &block)
logger_common(*args, level: level, **options, &block)
end
##
# @overload log_depth(depth: 1, **options, &block)
# Increase depth around a method invocation
# @param [Integer] :depth Additional recursion depth
# @param [Hash{Symbol}] options (@options || {})
# @option options [Logger, #<<] :logger
# @yield
# Yields with no arguments
# @yieldreturn [Object] returns the result of yielding
# @return [Object]
#
# @overload log_depth
# # Return the current log depth
# @return [Integer]
def log_depth(depth: 1, **options, &block)
self.logger(**options).log_depth(depth: depth, &block)
end
private
LOGGER_COMMON_LEVELS = {
fatal: 4, error: 3, warn: 2, info: 1, debug: 0
}.freeze
LOGGER_COMMON_LEVELS_REVERSE = LOGGER_COMMON_LEVELS.invert.freeze
##
# Common method for logging messages
#
# The call is ignored, unless `@logger` or `@options[:logger]` is set, in which case it records tracing information as indicated.
#
# @overload logger_common(args, options)
# @param [Array<String>] args Messages
# @param [:fatal, :error, :warn, :info, :debug] level
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :depth
# Recursion depth for indenting output
# @option options [:fatal, :error, :warn, :info, :debug] level (:<<)
# @option options [Integer] :lineno associated with message
# @option options [Logger, #<<] :logger
# @yieldreturn [String] added to message
# @return [void]
def logger_common(*args, level:, **options)
logger = self.logger(**options)
# Some older code uses integer level numbers
level = LOGGER_COMMON_LEVELS_REVERSE.fetch(level) if level.is_a?(Integer)
logger.log_statistics[level] = logger.log_statistics[level].to_i + 1
return if logger.level > LOGGER_COMMON_LEVELS.fetch(level)
depth = options.fetch(:depth, logger.log_depth)
args << yield if block_given?
str = (depth > 100 ? ' ' * 100 + '+' : ' ' * depth) + args.join(": ")
str = "[line #{options[:lineno]}] #{str}" if options[:lineno]
logger.send(level, str)
end
##
# Module which is mixed-in to found logger to provide statistics and depth behavior
module LoggerBehavior
attr_accessor :recovering
def log_statistics
@logger_statistics ||= {}
end
##
# @overload log_depth(depth: 1, **options, &block)
# Increase depth around a method invocation
# @param [Integer] depth (1) recursion depth
# @param [Hash{Symbol}] options (@options || {})
# @option options [Logger, #<<] :logger
# @yield
# Yields with no arguments
# @yieldreturn [Object] returns the result of yielding
# @return [Object]
#
# @overload log_depth
# # Return the current log depth
# @return [Integer]
def log_depth(depth: 1, **options)
@log_depth ||= 0
if block_given?
@log_depth += depth
yield
else
@log_depth
end
ensure
@log_depth -= depth if block_given?
end
# Give Logger like behavior to non-logger objects
def method_missing(method, *args)
case method.to_sym
when :fatal, :error, :warn, :info, :debug
if self.respond_to?(:write)
self.write "#{method.to_s.upcase} #{(args.join(": "))}\n"
elsif self.respond_to?(:<<)
self << "#{method.to_s.upcase} #{args.join(": ")}"
else
# Silently eat the message
end
when :level, :sev_threshold then 2
else
super
end
end
def respond_to_missing?(name, include_private = false)
return true if
%i(fatal error warn info debug level sev_threshold)
.include?(name.to_sym)
super
end
end
end # Logger
end; end # RDF::Util