-
Notifications
You must be signed in to change notification settings - Fork 79
/
vmdb-logger.rb
179 lines (144 loc) · 5.08 KB
/
vmdb-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
require 'logger'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/numeric/bytes'
require 'active_support/core_ext/string'
require 'English'
class VMDBLogger < Logger
MAX_LOG_LINE_LENGTH = 1.megabyte
def initialize(*args)
super
self.level = INFO
# HACK: ActiveSupport monkey patches the standard Ruby Logger#initialize
# method to set @formatter to a SimpleFormatter.
#
# The ActiveSupport Logger patches are deprecated in Rails 3.1.1 in favor of
# ActiveSupport::BufferedLogger, so this hack may not be needed in future
# version of Rails.
self.formatter = Formatter.new
# Allow for thread safe Logger level changes, similar to functionalities
# provided by ActiveSupport::LoggerThreadSafeLevel
@write_lock = Mutex.new
@local_levels = {}
end
# Silences the logger for the duration of the block.
#
# Taken from activesupport/logger_silence
def silence(temporary_level = Logger::ERROR)
old_local_level = local_level
self.local_level = temporary_level
yield self
ensure
self.local_level = old_local_level
end
attr_reader :logdev # Expose logdev
def logdev=(logdev)
if @logdev
shift_age = @logdev.instance_variable_get(:@shift_age)
shift_size = @logdev.instance_variable_get(:@shift_size)
@logdev.close
else
shift_age = 0
shift_size = 1048576
end
@logdev = LogDevice.new(logdev, :shift_age => shift_age, :shift_size => shift_size)
end
def filename
logdev.filename unless logdev.nil?
end
alias_method :filename=, :logdev=
def self.contents(log, width = nil, last = 1000)
return "" unless File.file?(log)
if last.nil?
contents = File.open(log, "rb", &:read).split("\n")
else
require 'util/miq-system'
contents = MiqSystem.tail(log, last)
end
return "" if contents.nil? || contents.empty?
results = []
# Wrap lines at width if passed
contents.each do |line|
while !width.nil? && line.length > width
# Don't return lines containing invalid UTF8 byte sequences - see vmdb_logger_test.rb
results.push(line[0...width]) if (line[0...width].unpack("U*") rescue nil)
line = line[width..line.length]
end
# Don't return lines containing invalid UTF8 byte sequences - see vmdb_logger_test.rb
results.push(line) if line.length && (line.unpack("U*") rescue nil)
end
# Put back the utf-8 encoding which is the default for most rails libraries
# after opening it as binary and getting rid of the invalid UTF8 byte sequences
results.join("\n").force_encoding("utf-8")
end
def contents(width = nil, last = 1000)
self.class.contents(filename, width, last)
end
def log_backtrace(err, level = :error)
# Get the name of the method that called us unless it is a wrapped log_backtrace
method_name = nil
caller.each do |c|
method_name = c[/`([^']*)'/, 1]
break unless method_name == 'log_backtrace'
end
# Log the error text
send(level, "[#{err.class.name}]: #{err.message} Method:[#{method_name}]")
# Log the stack trace except for some specific exceptions
unless (Object.const_defined?(:MiqException) && err.kind_of?(MiqException::Error)) ||
(Object.const_defined?(:MiqAeException) && err.kind_of?(MiqAeException::Error))
send(level, err.backtrace.nil? || err.backtrace.empty? ? "Backtrace is not available" : err.backtrace.join("\n"))
end
end
def self.log_hashes(logger, h, options = {})
require 'yaml'
require 'miq-password'
level = options[:log_level] || :info
filter = Array(options[:filter]).flatten.compact.map(&:to_s) << "password"
filter.uniq!
values = YAML.dump(h.to_hash).gsub(MiqPassword::REGEXP, "[FILTERED]")
values = values.split("\n").map do |l|
if (key = filter.detect { |f| l.include?(f) })
l.gsub!(/#{key}.*: (.+)/) { |m| m.gsub!($1, "[FILTERED]") }
end
l
end.join("\n")
logger.send(level, "\n#{values}")
end
def log_hashes(h, options = {})
self.class.log_hashes(self, h, options)
end
def level
local_level || super
end
private
def local_log_id
Thread.current.object_id
end
def local_level
@local_levels[local_log_id]
end
def local_level=(level)
@write_lock.synchronize do
if level
@local_levels[local_log_id] = level
else
@local_levels.delete(local_log_id)
end
end
end
class Formatter < Logger::Formatter
FORMAT = "[----] %s, [%s#%d:%x] %5s -- %s: %s\n"
def call(severity, time, progname, msg)
msg = prefix_task_id(msg2str(msg)).truncate(MAX_LOG_LINE_LENGTH)
FORMAT % [severity[0..0], format_datetime(time), $PROCESS_ID, Thread.current.object_id, severity, progname, msg]
end
private
def prefix_task_id(msg)
# Add task id to the message if a task is currently being worked on.
if (task_id = (Thread.current["tracking_label"] || $_miq_worker_current_msg.try(:task_id)))
prefix = "Q-task_id([#{task_id}])"
msg = "#{prefix} #{msg}" unless msg.include?(prefix)
end
msg
end
end
end