-
Notifications
You must be signed in to change notification settings - Fork 898
/
logging.rb
61 lines (51 loc) · 1.32 KB
/
logging.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
require 'vmdb/loggers'
module Vmdb
class LogProxy < Struct.new(:klass, :separator, :prefix_cache)
LEVELS = [:debug, :info, :warn, :error]
# def debug?
# def info?
# def warn?
# def error?
# def add
# def level
# def log_backtrace
(LEVELS.map { |l| :"#{l}?" } + %i[add level log_backtrace]).each do |method|
define_method(method) { |*args| logger.send(method, *args) }
end
LEVELS.each do |level|
define_method(level) do |msg = nil, &blk|
location = caller_locations(1, 1)
if blk
logger.send(level) do
"#{prefix(location)} #{blk.call}"
end
else
logger.send(level, "#{prefix(location)} #{msg}")
end
end
end
def prefix(location = caller_locations(1, 1))
location = location.first if location.kind_of?(Array)
meth = location.base_label
prefix_cache[meth] ||= meth ? "MIQ(#{klass}#{separator}#{meth})" : "MIQ(#{klass})"
end
private
def logger
Vmdb.logger
end
end
module Logging
def _log
self.class.instance_logger
end
end
module ClassLogging
def instance_logger
@instance_logger ||= LogProxy.new(name, '#', {})
end
def _log
@_log ||= LogProxy.new(name, '.', {})
end
end
::Module.include ClassLogging
end