Created
November 27, 2026 19:29
-
-
Save robbat2/fce99cab20b3dee0752ef91b471e06e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Robin's hack to get quieter Puppet most of the time | |
| # On a run where nothing changes, there are 3 NOTICE log messages, and nothing else: | |
| # Requesting catalog from ... | |
| # Catalog compiled by ... | |
| # Applied catalog in ... | |
| # | |
| # If there ARE changes, there are also other messages at the NOTICE level, that we want to keep | |
| # So we need to conditionally change the level of the above messages. | |
| # | |
| # However, we can't just buffer blindly; if the agent crashes, a buffer would be lost. | |
| # So buffer until any OTHER message is seen at a required level. | |
| # | |
| # .../lib/puppet/feature/quiet_catalog_logs.rb | |
| require 'puppet/util/log' | |
| unless Puppet::Util::Log.singleton_class.method_defined?(:orig_create) | |
| Puppet::Util::Log.singleton_class.alias_method :orig_create, :create | |
| @buffered_lifecycle_logs = [] | |
| @run_had_changes = false | |
| Puppet::Util::Log.define_singleton_method(:create) do |args| | |
| # Bypass buffering completely if the agent is running at :info or :debug | |
| if [:debug, :info].include?(Puppet::Util::Log.level) | |
| # Flush anything previously buffered just in case log level shifted mid-run | |
| unless @buffered_lifecycle_logs.empty? | |
| @buffered_lifecycle_logs.each { |buffered| orig_create(buffered) } | |
| @buffered_lifecycle_logs.clear | |
| end | |
| return orig_create(args) | |
| end | |
| # If we get here, the agent log_level is set to notice or higher. | |
| msg = args[:message].to_s | |
| level = args[:level] | |
| is_lifecycle = (level == :notice) && [ | |
| /\ARequesting catalog from /, | |
| /\ACatalog compiled by /, | |
| /\AApplied catalog in / | |
| ].any? { |r| msg.match?(r) } | |
| if is_lifecycle | |
| # Reset buffer at the start of a new catalog request | |
| if msg.start_with?('Requesting catalog from ') | |
| @buffered_lifecycle_logs.clear | |
| @run_had_changes = false | |
| end | |
| if @run_had_changes | |
| # A change already occurred earlier in this run; emit normally | |
| orig_create(args) | |
| else | |
| # Hold in memory; downgrade to :info if emitted immediately, or store for later | |
| @buffered_lifecycle_logs << args.dup | |
| nil | |
| end | |
| else | |
| # If any other :notice (a resource change) or :err occurs, flush the buffer first | |
| if [:notice, :warning, :err, :alert, :emerg, :crit].include?(level) && !@run_had_changes | |
| @run_had_changes = true | |
| @buffered_lifecycle_logs.each { |buffered| orig_create(buffered) } | |
| @buffered_lifecycle_logs.clear | |
| end | |
| orig_create(args) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment