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
ALNUM, ALPHA, AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE, ESCAPED, HEX, HttpProxy, PUPPET_STACK_INSERTION_FRAME, RESERVED, RFC_3986_URI_REGEX, UNRESERVED, UNSAFE
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
- .close_all ⇒ Object
- .from_data_hash(data) ⇒ Object
-
.log_func(scope, level, vals) ⇒ Object
Log output using scope and level.
-
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
- .setup_default ⇒ Object
-
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?.
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, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask
Methods included from POSIX
#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from SymbolicFileMode
#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Methods included from ClassGen
Methods included from Network::FormatSupport
included, #mime, #render, #support_format?, #to_json, #to_msgpack, #to_pson
Methods included from Tagging
#merge_into, #merge_tags_from, #raw_tagged?, #set_tags, #tag, #tag_if_valid, #tagged?, #tags, #tags=, #valid_tag?
Methods included from PsychSupport
Constructor Details
#initialize(args) ⇒ Log
Returns a new instance of Log.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/puppet/util/log.rb', line 306 def initialize(args) self.level = args[:level] self. = args[:message] self.source = args[:source] || "Puppet" @time = Time.now = args[:tags] if .each { |t| 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| value = args[attr] next unless value 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.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def backtrace @backtrace end |
#environment ⇒ Object
Returns the value of attribute environment.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def environment @environment end |
#file ⇒ Object
Returns the value of attribute file.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def file @file end |
#issue_code ⇒ Object
Returns the value of attribute issue_code.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def issue_code @issue_code end |
#level ⇒ Object
Returns the value of attribute level.
304 305 306 |
# File 'lib/puppet/util/log.rb', line 304 def level @level end |
#line ⇒ Object
Returns the value of attribute line.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def line @line end |
#message ⇒ Object
Returns the value of attribute message.
304 305 306 |
# File 'lib/puppet/util/log.rb', line 304 def @message end |
#node ⇒ Object
Returns the value of attribute node.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def node @node end |
#pos ⇒ Object
Returns the value of attribute pos.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def pos @pos end |
#remote ⇒ Object
Returns the value of attribute remote.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def remote @remote end |
#source ⇒ Object
Returns the value of attribute source.
304 305 306 |
# File 'lib/puppet/util/log.rb', line 304 def source @source end |
#time ⇒ Object
Returns the value of attribute time.
303 304 305 |
# File 'lib/puppet/util/log.rb', line 303 def time @time end |
Class Method Details
.close_all ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/puppet/util/log.rb', line 62 def self.close_all @destinations.keys.each { |dest| close(dest) } # TRANSLATORS "Log.close_all" is a method name and should not be translated raise Puppet::DevError, _("Log.close_all failed to close %{destinations}") % { destinations: @destinations.keys.inspect } unless @destinations.empty? end |
.from_data_hash(data) ⇒ Object
273 274 275 276 277 |
# File 'lib/puppet/util/log.rb', line 273 def self.from_data_hash(data) obj = allocate obj.initialize_from_hash(data) obj end |
.log_func(scope, level, vals) ⇒ Object
Log output using scope and level
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/puppet/util/log.rb', line 285 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 |
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puppet/util/log.rb', line 24 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 |
.setup_default ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/puppet/util/log.rb', line 258 def self.setup_default Log.newdestination( if Puppet.features.syslog? :syslog else Puppet.features.eventlog? ? :eventlog : Puppet[:puppetdlog] end ) end |
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?
269 270 271 |
# File 'lib/puppet/util/log.rb', line 269 def self.validlevel?(level) @levels.include?(level) end |
Instance Method Details
#initialize_from_hash(data) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/puppet/util/log.rb', line 329 def initialize_from_hash(data) @level = data['level'].intern @message = data['message'] @source = data['source'] @tags = 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| value = data[name] next unless value send(name + '=', value) end end |
#to_data_hash ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/puppet/util/log.rb', line 351 def to_data_hash { 'level' => @level.to_s, 'message' => to_s, 'source' => @source, 'tags' => @tags.to_a, 'time' => @time.iso8601(9), 'file' => @file, 'line' => @line, } end |
#to_hash ⇒ Object
347 348 349 |
# File 'lib/puppet/util/log.rb', line 347 def to_hash to_data_hash end |
#to_report ⇒ Object
411 412 413 |
# File 'lib/puppet/util/log.rb', line 411 def to_report "#{time} #{source} (#{level}): #{self}" end |
#to_s ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/puppet/util/log.rb', line 415 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 %{environment}: %{msg}") % { environment: environment, msg: msg } unless environment.nil? msg += Puppet::Util::Errors.error_location_with_space(file, line, pos) 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
363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/puppet/util/log.rb', line 363 def to_structured_hash hash = { 'level' => @level, 'message' => @message, 'source' => @source, 'tags' => @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 |