Class: Puppet::Util::Log
- Extended by:
- Puppet::Util, ClassGen
- Includes:
- Network::FormatSupport, Puppet::Util, PsychSupport, Tagging
- Defined in:
- lib/puppet/util/log.rb
Overview
Pass feedback to the user. Log levels are modeled after syslog’s, and it is expected that that will be the most common log destination. Supports multiple destinations, one of which is a remote server.
Defined Under Namespace
Classes: Destination
Constant Summary
Constants included from Puppet::Util
AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE
Constants included from POSIX
POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS
Constants included from SymbolicFileMode
SymbolicFileMode::SetGIDBit, SymbolicFileMode::SetUIDBit, SymbolicFileMode::StickyBit, SymbolicFileMode::SymbolicMode, SymbolicFileMode::SymbolicSpecialToBit
Constants included from Tagging
Class Attribute Summary collapse
-
.desttypes ⇒ Object
readonly
Returns the value of attribute desttypes.
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#file ⇒ Object
Returns the value of attribute file.
-
#issue_code ⇒ Object
Returns the value of attribute issue_code.
-
#level ⇒ Object
Returns the value of attribute level.
-
#line ⇒ Object
Returns the value of attribute line.
-
#message ⇒ Object
Returns the value of attribute message.
-
#node ⇒ Object
Returns the value of attribute node.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#remote ⇒ Object
Returns the value of attribute remote.
-
#source ⇒ Object
Returns the value of attribute source.
-
#time ⇒ Object
Returns the value of attribute time.
Class Method Summary collapse
- .autoflush=(v) ⇒ Object
-
.close(destination) ⇒ Object
Reset log to basics.
- .close_all ⇒ Object
-
.create(hash) ⇒ Object
Create a new log message.
- .destinations ⇒ Object
-
.eachlevel ⇒ Object
Yield each valid level in turn.
-
.flush ⇒ Object
Flush any log destinations that support such operations.
- .flushqueue ⇒ Object
-
.force_flushqueue ⇒ Object
Flush the logging queue.
- .from_data_hash(data) ⇒ Object
-
.level ⇒ Object
Return the current log level.
-
.level=(level) ⇒ Object
Set the current log level.
- .levels ⇒ Object
-
.log_func(scope, level, vals) ⇒ Object
Log output using scope and level.
-
.newdestination(dest) ⇒ Object
Create a new log destination.
-
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
-
.newmessage(msg) ⇒ Object
Route the actual message.
- .queuemessage(msg) ⇒ Object
-
.reopen ⇒ Object
Reopen all of our logs.
- .sendlevel?(level) ⇒ Boolean
- .setup_default ⇒ Object
-
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?.
- .with_destination(destination, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(args) ⇒ Log
constructor
A new instance of Log.
- #initialize_from_hash(data) ⇒ Object
- #to_data_hash ⇒ Object
- #to_hash ⇒ Object
- #to_report ⇒ Object
- #to_s ⇒ Object
- #to_structured_hash ⇒ Object
Methods included from Puppet::Util
absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask
Methods included from POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from SymbolicFileMode
#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Methods included from ClassGen
Methods included from MethodHelper
#requiredopts, #set_options, #symbolize_options
Methods included from Network::FormatSupport
included, #mime, #render, #support_format?, #to_json, #to_msgpack, #to_pson
Methods included from Tagging
#merge_into, #merge_tags, #raw_tagged?, #set_tags, #tag, #tag_if_valid, #tagged?, #tags, #tags=
Methods included from PsychSupport
Constructor Details
#initialize(args) ⇒ Log
Returns a new instance of Log.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/puppet/util/log.rb', line 287 def initialize(args) self.level = args[:level] self. = args[:message] self.source = args[:source] || "Puppet" @time = Time.now if = args[:tags] .each { |t| self.tag(t) } end # Don't add these unless defined (preserve 3.x API as much as possible) [:file, :line, :pos, :issue_code, :environment, :node, :backtrace].each do |attr| next unless value = args[attr] send(attr.to_s + '=', value) end Log.(self) end |
Class Attribute Details
.desttypes ⇒ Object (readonly)
Returns the value of attribute desttypes.
49 50 51 |
# File 'lib/puppet/util/log.rb', line 49 def desttypes @desttypes end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def backtrace @backtrace end |
#environment ⇒ Object
Returns the value of attribute environment.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def environment @environment end |
#file ⇒ Object
Returns the value of attribute file.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def file @file end |
#issue_code ⇒ Object
Returns the value of attribute issue_code.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def issue_code @issue_code end |
#level ⇒ Object
Returns the value of attribute level.
285 286 287 |
# File 'lib/puppet/util/log.rb', line 285 def level @level end |
#line ⇒ Object
Returns the value of attribute line.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def line @line end |
#message ⇒ Object
Returns the value of attribute message.
285 286 287 |
# File 'lib/puppet/util/log.rb', line 285 def end |
#node ⇒ Object
Returns the value of attribute node.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def node @node end |
#pos ⇒ Object
Returns the value of attribute pos.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def pos @pos end |
#remote ⇒ Object
Returns the value of attribute remote.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def remote @remote end |
#source ⇒ Object
Returns the value of attribute source.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def source @source end |
#time ⇒ Object
Returns the value of attribute time.
284 285 286 |
# File 'lib/puppet/util/log.rb', line 284 def time @time end |
Class Method Details
.autoflush=(v) ⇒ Object
76 77 78 79 80 |
# File 'lib/puppet/util/log.rb', line 76 def Log.autoflush=(v) @destinations.each do |type, dest| dest.autoflush = v if dest.respond_to?(:autoflush=) end end |
.close(destination) ⇒ Object
Reset log to basics. Basically just flushes and closes files and undefs other objects.
54 55 56 57 58 59 60 |
# File 'lib/puppet/util/log.rb', line 54 def Log.close(destination) if @destinations.include?(destination) @destinations[destination].flush if @destinations[destination].respond_to?(:flush) @destinations[destination].close if @destinations[destination].respond_to?(:close) @destinations.delete(destination) end end |
.close_all ⇒ Object
62 63 64 65 66 67 |
# File 'lib/puppet/util/log.rb', line 62 def self.close_all destinations.keys.each { |dest| close(dest) } raise Puppet::DevError.new("Log.close_all failed to close #{@destinations.keys.inspect}") if !@destinations.empty? end |
.create(hash) ⇒ Object
Create a new log message. The primary role of this method is to avoid creating log messages below the loglevel.
84 85 86 87 88 |
# File 'lib/puppet/util/log.rb', line 84 def Log.create(hash) raise Puppet::DevError, "Logs require a level" unless hash.include?(:level) raise Puppet::DevError, "Invalid log level #{hash[:level]}" unless @levels.index(hash[:level]) @levels.index(hash[:level]) >= @loglevel ? Puppet::Util::Log.new(hash) : nil end |
.destinations ⇒ Object
90 91 92 |
# File 'lib/puppet/util/log.rb', line 90 def Log.destinations @destinations end |
.eachlevel ⇒ Object
Yield each valid level in turn
95 96 97 |
# File 'lib/puppet/util/log.rb', line 95 def Log.eachlevel @levels.each { |level| yield level } end |
.flush ⇒ Object
Flush any log destinations that support such operations.
70 71 72 73 74 |
# File 'lib/puppet/util/log.rb', line 70 def Log.flush @destinations.each { |type, dest| dest.flush if dest.respond_to?(:flush) } end |
.flushqueue ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/puppet/util/log.rb', line 195 def Log.flushqueue return unless @destinations.size >= 1 @queued.each do |msg| Log.(msg) end @queued.clear end |
.force_flushqueue ⇒ Object
Flush the logging queue. If there are no destinations available,
adds in a console logger before flushing the queue.
This is mainly intended to be used as a last-resort attempt
to ensure that logging are not thrown away before
the program is about to exit--most likely in a horrific
error scenario.
210 211 212 213 214 215 |
# File 'lib/puppet/util/log.rb', line 210 def Log.force_flushqueue() if (@destinations.empty? and !(@queued.empty?)) newdestination(:console) end flushqueue end |
.from_data_hash(data) ⇒ Object
253 254 255 256 257 |
# File 'lib/puppet/util/log.rb', line 253 def self.from_data_hash(data) obj = allocate obj.initialize_from_hash(data) obj end |
.level ⇒ Object
Return the current log level.
100 101 102 |
# File 'lib/puppet/util/log.rb', line 100 def Log.level @levels[@loglevel] end |
.level=(level) ⇒ Object
Set the current log level.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/puppet/util/log.rb', line 105 def Log.level=(level) level = level.intern unless level.is_a?(Symbol) raise Puppet::DevError, "Invalid loglevel #{level}" unless @levels.include?(level) @loglevel = @levels.index(level) # Enable or disable Facter debugging Facter.debugging(level == :debug) if Facter.respond_to? :debugging end |
.levels ⇒ Object
116 117 118 |
# File 'lib/puppet/util/log.rb', line 116 def Log.levels @levels.dup end |
.log_func(scope, level, vals) ⇒ Object
Log output using scope and level
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/puppet/util/log.rb', line 265 def self.log_func(scope, level, vals) # NOTE: 3x, does this: vals.join(" ") # New implementation uses the evaluator to get proper formatting per type vals = vals.map { |v| Puppet::Pops::Evaluator::EvaluatorImpl.new.string(v, scope) } # Bypass Puppet.<level> call since it picks up source from "self" which is not applicable in the 4x # Function API. # TODO: When a function can obtain the file, line, pos of the call merge those in (3x supports # options :file, :line. (These were never output when calling the 3x logging functions since # 3x scope does not know about the calling location at that detailed level, nor do they # appear in a report to stdout/error when included). Now, the output simply uses scope (like 3x) # as this is good enough, but does not reflect the true call-stack, but is a rough estimate # of where the logging call originates from). # Puppet::Util::Log.create({:level => level, :source => scope, :message => vals.join(" ")}) nil end |
.newdestination(dest) ⇒ Object
Create a new log destination.
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 |
# File 'lib/puppet/util/log.rb', line 121 def Log.newdestination(dest) # Each destination can only occur once. if @destinations.find { |name, obj| obj.name == dest } return end name, type = @desttypes.find do |name, klass| klass.match?(dest) end if type.respond_to?(:suitable?) and not type.suitable?(dest) return end raise Puppet::DevError, "Unknown destination type #{dest}" unless type begin if type.instance_method(:initialize).arity == 1 @destinations[dest] = type.new(dest) else @destinations[dest] = type.new end flushqueue @destinations[dest] rescue => detail Puppet.log_exception(detail) # If this was our only destination, then add the console back in. newdestination(:console) if @destinations.empty? and (dest != :console and dest != "console") end end |
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puppet/util/log.rb', line 23 def self.newdesttype(name, = {}, &block) dest = genclass( name, :parent => Puppet::Util::Log::Destination, :prefix => "Dest", :block => block, :hash => @desttypes, :attributes => ) dest.match(dest.name) dest end |
.newmessage(msg) ⇒ Object
Route the actual message. FIXME There are lots of things this method should do, like caching and a bit more. It’s worth noting that there’s a potential for a loop here, if the machine somehow gets the destination set as itself.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puppet/util/log.rb', line 170 def Log.(msg) return if @levels.index(msg.level) < @loglevel if msg..valid_encoding? = Puppet::Util::CharacterEncoding.convert_to_utf_8(msg.) else = _("Received a log message with invalid encoding:") << Puppet::Util::CharacterEncoding.convert_to_utf_8(msg..dump) << "\n" # We only select the last 10 callers in the stack to avoid being spammy << _("Backtrace:") << "\n" << caller[0..10].join("\n") end msg. = (msg) if @destinations.length == 0 @destinations.each do |name, dest| dest.handle(msg) end end |
.queuemessage(msg) ⇒ Object
191 192 193 |
# File 'lib/puppet/util/log.rb', line 191 def Log.(msg) @queued.push(msg) end |
.reopen ⇒ Object
Reopen all of our logs.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/puppet/util/log.rb', line 222 def Log.reopen Puppet.notice _("Reopening log files") types = @destinations.keys @destinations.each { |type, dest| dest.close if dest.respond_to?(:close) } @destinations.clear # We need to make sure we always end up with some kind of destination begin types.each { |type| Log.newdestination(type) } rescue => detail if @destinations.empty? Log.setup_default Puppet.err detail.to_s end end end |
.sendlevel?(level) ⇒ Boolean
217 218 219 |
# File 'lib/puppet/util/log.rb', line 217 def Log.sendlevel?(level) @levels.index(level) >= @loglevel end |
.setup_default ⇒ Object
242 243 244 245 246 |
# File 'lib/puppet/util/log.rb', line 242 def self.setup_default Log.newdestination( (Puppet.features.syslog? ? :syslog : (Puppet.features.eventlog? ? :eventlog : Puppet[:puppetdlog]))) end |
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?
249 250 251 |
# File 'lib/puppet/util/log.rb', line 249 def self.validlevel?(level) @levels.include?(level) end |
.with_destination(destination, &block) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/puppet/util/log.rb', line 153 def Log.with_destination(destination, &block) if @destinations.include?(destination) yield else newdestination(destination) begin yield ensure close(destination) end end end |
Instance Method Details
#initialize_from_hash(data) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/puppet/util/log.rb', line 307 def initialize_from_hash(data) @level = data['level'].intern = data['message'] @source = data['source'] = Puppet::Util::TagSet.new(data['tags']) @time = data['time'] if @time.is_a? String @time = Time.parse(@time) end # Don't add these unless defined (preserve 3.x API as much as possible) %w(file line pos issue_code environment node backtrace).each do |name| next unless value = data[name] send(name + '=', value) end end |
#to_data_hash ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/puppet/util/log.rb', line 327 def to_data_hash { 'level' => @level.to_s, 'message' => to_s, 'source' => @source, 'tags' => .to_a, 'time' => @time.iso8601(9), 'file' => @file, 'line' => @line, } end |
#to_hash ⇒ Object
323 324 325 |
# File 'lib/puppet/util/log.rb', line 323 def to_hash self.to_data_hash end |
#to_report ⇒ Object
382 383 384 |
# File 'lib/puppet/util/log.rb', line 382 def to_report "#{time} #{source} (#{level}): #{to_s}" end |
#to_s ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/puppet/util/log.rb', line 386 def to_s msg = # Issue based messages do not have details in the message. It # must be appended here unless issue_code.nil? msg = _("Could not parse for environment %{env}: %{msg}") % { env: environment, msg: msg } unless environment.nil? if file && line && pos msg = _("%{msg} at %{file}:%{line}:%{pos}") % { msg: msg, file: file, line: line, pos: pos } elsif file and line msg = _("%{msg} at %{file}:%{line}") % { msg: msg, file: file, line: line } elsif line && pos msg = _("%{msg} at line %{line}:%{pos}") % { msg: msg, line: line, pos: pos } elsif line msg = _("%{msg} at line %{line}") % { msg: msg, line: line } elsif file msg = _("%{msg} in %{file}") % { msg: msg, file: file } end msg = _("%{msg} on node %{node}") % { msg: msg, node: node } unless node.nil? if @backtrace.is_a?(Array) msg += "\n" msg += @backtrace.join("\n") end end msg end |
#to_structured_hash ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/puppet/util/log.rb', line 339 def to_structured_hash hash = { 'level' => @level, 'message' => , 'source' => @source, 'tags' => .to_a, 'time' => @time.iso8601(9), } %w(file line pos issue_code environment node backtrace).each do |name| attr_name = "@#{name}" hash[name] = instance_variable_get(attr_name) if instance_variable_defined?(attr_name) end hash end |