Module: Puppet::Util::Logging
- Included in:
- Puppet, FileServing::Mount, Module, Network::AuthStore, Parameter, Parser::Resource, Provider, Resource::Status, Transaction::Event, Type, Type, Puppet::Util::Log::RateLimitedLogger, ResourceTemplate
- Defined in:
- lib/puppet/util/logging.rb
Defined Under Namespace
Classes: DeprecationWarning
Instance Method Summary collapse
- #clear_deprecation_warnings ⇒ Object
-
#deprecation_warning(message, key = nil) ⇒ Object
Logs a warning indicating that the Ruby code path is deprecated.
- #format_exception(exception, message = :default, trace = true) ⇒ Object
- #get_deprecation_offender ⇒ Object
- #log_and_raise(exception, message) ⇒ Object
-
#log_deprecations_to_file(deprecations_file, pattern = nil) ⇒ Object
utility method that can be called, e.g., from spec_helper config.after, when tracking down calls to deprecated code.
-
#log_exception(exception, message = :default, options = {}) ⇒ Object
Log an exception via Puppet.err.
-
#puppet_deprecation_warning(message, options = {}) ⇒ Object
Logs a warning whose origin comes from Puppet source rather than somewhere internal within Puppet.
- #send_log(level, message) ⇒ Object
Instance Method Details
#clear_deprecation_warnings ⇒ Object
107 108 109 |
# File 'lib/puppet/util/logging.rb', line 107 def clear_deprecation_warnings $deprecation_warnings.clear if $deprecation_warnings end |
#deprecation_warning(message, key = nil) ⇒ Object
Logs a warning indicating that the Ruby code path is deprecated. Note that this method keeps track of the offending lines of code that triggered the deprecation warning, and will only log a warning once per offending line of code. It will also stop logging deprecation warnings altogether after 100 unique deprecation warnings have been logged. Finally, if Puppet includes ‘deprecations’, it will squelch all warning calls made via this method.
69 70 71 |
# File 'lib/puppet/util/logging.rb', line 69 def deprecation_warning(, key = nil) issue_deprecation_warning(, key, nil, nil, true) end |
#format_exception(exception, message = :default, trace = true) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/puppet/util/logging.rb', line 30 def format_exception(exception, = :default, trace = true) arr = [] case when :default arr << exception. when nil # don't log anything if they passed a nil; they are just calling for the optional backtrace logging else arr << end if trace and exception.backtrace arr << Puppet::Util.pretty_backtrace(exception.backtrace) end if exception.respond_to?(:original) and exception.original arr << "Wrapped exception:" arr << format_exception(exception.original, :default, trace) end arr.flatten.join("\n") end |
#get_deprecation_offender ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/puppet/util/logging.rb', line 94 def get_deprecation_offender() # we have to put this in its own method to simplify testing; we need to be able to mock the offender results in # order to test this class, and our framework does not appear to enjoy it if you try to mock Kernel.caller # # let's find the offending line; we need to jump back up the stack a few steps to find the method that called # the deprecated method if Puppet[:trace] caller()[2..-1] else [caller()[2]] end end |
#log_and_raise(exception, message) ⇒ Object
51 52 53 54 |
# File 'lib/puppet/util/logging.rb', line 51 def log_and_raise(exception, ) log_exception(exception, ) raise exception, + "\n" + exception.to_s, exception.backtrace end |
#log_deprecations_to_file(deprecations_file, pattern = nil) ⇒ Object
utility method that can be called, e.g., from spec_helper config.after, when tracking down calls to deprecated code. Parameters:
- deprecations_file
-
relative or absolute path of a file to log the deprecations to
- pattern
-
(default nil) if specified, will only log deprecations whose message matches the provided pattern
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/puppet/util/logging.rb', line 119 def log_deprecations_to_file(deprecations_file, pattern = nil) # this method may get called lots and lots of times (e.g., from spec_helper config.after) without the global # list of deprecation warnings being cleared out. We don't want to keep logging the same offenders over and over, # so, we need to keep track of what we've logged. # # It'd be nice if we could just clear out the list of deprecation warnings, but then the very next spec might # find the same offender, and we'd end up logging it again. $logged_deprecation_warnings ||= {} File.open(deprecations_file, "a") do |f| if ($deprecation_warnings) then $deprecation_warnings.each do |offender, | if (! $logged_deprecation_warnings.has_key?(offender)) then $logged_deprecation_warnings[offender] = true if ((pattern.nil?) || ( =~ pattern)) then f.puts() f.puts(offender) f.puts() end end end end end end |
#log_exception(exception, message = :default, options = {}) ⇒ Object
Log an exception via Puppet.err. Will also log the backtrace if Puppet is set. Parameters:
- exception
-
an Exception to log
- message
-
an optional String overriding the message to be logged; by default, we log Exception.message.
If you pass a String here, your string will be logged instead. You may also pass nil if you don't
wish to log a message at all; in this case it is likely that you are only calling this method in order
to take advantage of the backtrace logging.
26 27 28 |
# File 'lib/puppet/util/logging.rb', line 26 def log_exception(exception, = :default, = {}) err(format_exception(exception, , Puppet[:trace] || [:trace])) end |
#puppet_deprecation_warning(message, options = {}) ⇒ Object
Logs a warning whose origin comes from Puppet source rather than somewhere internal within Puppet. Otherwise the same as deprecation_warning()
Either :file and :line and/or :key must be passed.
84 85 86 87 88 89 90 91 92 |
# File 'lib/puppet/util/logging.rb', line 84 def puppet_deprecation_warning(, = {}) key = [:key] file = [:file] line = [:line] raise(Puppet::DevError, "Need either :file and :line, or :key") if (key.nil?) && (file.nil? || line.nil?) key ||= "#{file}:#{line}" issue_deprecation_warning(, key, file, line, false) end |