Module: CemAcpt::Logging
- Included in:
- CemAcpt, Config, ConfigUtils, Context, ImageNameBuilder, NodeData, NodeInventory, Platform, Platform::Base, Platform::CmdBase, Platform::Gcp::VM, PuppetHelpers::Module, RunHandler, Runner, TestData::Fetcher
- Defined in:
- lib/cem_acpt/logging.rb
Overview
Logging for CemAcpt
Class Attribute Summary collapse
-
.logger ⇒ Logger
Exposes a logger instance.
Class Method Summary collapse
-
.current_log_config ⇒ Hash
Returns the current log config if set, or the default if not.
-
.current_log_format ⇒ Symbol
Shows the current log format style if set, or the default if not.
-
.current_log_level ⇒ Logger::Severity
Shortcut method for logger.level.
-
.included(base) ⇒ Object
Provides class method wrappers for logging methods.
-
.new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object
Creates a new log config hash and a new Logger instance using that config and sets the new Logger instance as the current logger.
-
.new_log_formatter(f) ⇒ Proc
Sets the current log format style and returns a proc to be passed to Logger#formatter=.
-
.new_log_level(level) ⇒ Object
Shortcut method to set logger.level.
Instance Method Summary collapse
-
#current_log_config ⇒ Object
Exposes the current log config.
-
#current_log_level ⇒ Object
Exposes the current log level.
-
#logger ⇒ Object
Exposes the logger instance.
-
#new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object
Exposes setting a new log config.
-
#new_log_level(level) ⇒ Object
Exposes setting the log level.
Class Attribute Details
.logger ⇒ Logger
Exposes a logger instance. Will either use the currently set logger or create a new one.
14 15 16 17 18 19 20 21 |
# File 'lib/cem_acpt/logging.rb', line 14 def logger @logger ||= Logger.new( current_log_config[:logdev], current_log_config[:shift_age], current_log_config[:shift_size], **current_log_config.reject { |k, _| %i[logdev shift_age shift_size].include?(k) } ) end |
Class Method Details
.current_log_config ⇒ Hash
Returns the current log config if set, or the default if not.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cem_acpt/logging.rb', line 88 def current_log_config return @log_config if @log_config formatter = new_log_formatter(current_log_format) @log_config = { logdev: $stdout, shift_age: 'o', shift_size: 1_048_576, level: Logger::INFO, progname: 'CemAcpt', datetime_format: '%Y%m%dT%H%M%S%z', formatter: formatter, } @log_config end |
.current_log_format ⇒ Symbol
Shows the current log format style if set, or the default if not.
52 53 54 |
# File 'lib/cem_acpt/logging.rb', line 52 def current_log_format @current_log_format ||= :text end |
.current_log_level ⇒ Logger::Severity
Shortcut method for logger.level
25 26 27 |
# File 'lib/cem_acpt/logging.rb', line 25 def current_log_level logger.level end |
.included(base) ⇒ Object
Provides class method wrappers for logging methods
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cem_acpt/logging.rb', line 144 def self.included(base) class << base def logger CemAcpt::Logging.logger end def current_log_level CemAcpt::Logging.current_log_level end def new_log_level(level) CemAcpt::Logging.new_log_level(level) end def current_log_config CemAcpt::Logging.current_log_config end def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) CemAcpt::Logging.new_log_config(logdev: logdev, shift_age: shift_age, shift_size: shift_size, level: level, formatter: formatter, datetime_format: datetime_format) end end end |
.new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object
Creates a new log config hash and a new Logger instance using that config and sets the new Logger instance as the current logger. NO DEFAULT VALUES ARE SET.
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 |
# File 'lib/cem_acpt/logging.rb', line 115 def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) @log_config[:shift_age] = shift_age if shift_age @log_config[:shift_size] = shift_size if shift_size @log_config[:level] = level if level @log_config[:formatter] = formatter if formatter @log_config[:datetime_format] = datetime_format if datetime_format case logdev when 'stdout' @log_config[:logdev] = $stdout when 'stderr' @log_config[:logdev] = $stderr when String @log_config[:logdev] = target when IO @log_config[:logdev] = logdev else @log_config[:logdev] = $stdout logger.warn("Unknown log target: #{logdev.inspect}, using STDOUT") end @logger = Logger.new( @log_config[:logdev], @log_config[:shift_age], @log_config[:shift_size], **@log_config.reject { |k, _| %i[logdev shift_age shift_size].include?(k) }, ) end |
.new_log_formatter(f) ⇒ Proc
Sets the current log format style and returns a proc to be passed to Logger#formatter=
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 |
# File 'lib/cem_acpt/logging.rb', line 60 def new_log_formatter(f) case f.downcase.to_sym when :json require 'json' @current_log_format = :json proc do |severity, datetime, progname, msg| { timestamp: datetime.iso8601(3), progname: progname, severity: severity, message: msg, }.to_json + "\n" end when :text @current_log_format = :text proc do |severity, _datetime, _progname, msg| "#{severity} - #{msg}\n" end else @current_log_format = :file proc do |severity, datetime, _progname, msg| "[#{datetime.iso8601(3)}] #{severity}: #{msg}\n" end end end |
.new_log_level(level) ⇒ Object
Shortcut method to set logger.level
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cem_acpt/logging.rb', line 31 def new_log_level(level) case level.downcase when 'debug' @logger.level = Logger::DEBUG when 'info' @logger.level = Logger::INFO when 'warn' @logger.level = Logger::WARN when 'error' @logger.level = Logger::ERROR when 'fatal' @logger.level = Logger::FATAL when 'unknown' @logger.level = Logger::UNKNOWN else raise "Cannot set log level #{level}: invalid level" end end |
Instance Method Details
#current_log_config ⇒ Object
Exposes the current log config
184 185 186 |
# File 'lib/cem_acpt/logging.rb', line 184 def current_log_config CemAcpt::Logging.current_log_config end |
#current_log_level ⇒ Object
Exposes the current log level
174 175 176 |
# File 'lib/cem_acpt/logging.rb', line 174 def current_log_level CemAcpt::Logging.current_log_level end |
#logger ⇒ Object
Exposes the logger instance
169 170 171 |
# File 'lib/cem_acpt/logging.rb', line 169 def logger CemAcpt::Logging.logger end |
#new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object
Exposes setting a new log config
189 190 191 |
# File 'lib/cem_acpt/logging.rb', line 189 def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) CemAcpt::Logging.new_log_config(logdev: logdev, shift_age: shift_age, shift_size: shift_size, level: level, formatter: formatter, datetime_format: datetime_format) end |
#new_log_level(level) ⇒ Object
Exposes setting the log level
179 180 181 |
# File 'lib/cem_acpt/logging.rb', line 179 def new_log_level(level) CemAcpt::Logging.new_log_level(level) end |