Module: CemAcpt::Logging
- Included in:
- CemAcpt, Actions, Bolt::Cmd::Base, Bolt::TaskList, Bolt::TaskWrapper, Bolt::TestRunner, Bolt::Tests::Test, Bolt::Tests::TestList, Bolt::YamlFile, Goss::Api, ImageBuilder::Exec::Gcloud, ImageBuilder::TerraformBuilder, ImageNameBuilder, Platform, Platform::Base, Provision, Provision::OsData, Provision::OsData, Provision::Terraform, Provision::TerraformCmd, PuppetHelpers::Module, TestData::Fetcher, TestRunner::Runner, Utils, Utils::Files::FileUtil, Utils::Puppet::ModulePackageBuilder, Utils::SSH::Keygen, Utils::WinRMRunner::RunnerWrapper, Utils::WinRMRunner::WinNode
- Defined in:
- lib/cem_acpt/logging.rb,
lib/cem_acpt/logging/formatter.rb
Overview
Logging for CemAcpt
Defined Under Namespace
Modules: Formatter Classes: Logger, MultiLogger
Constant Summary collapse
- LEVEL_MAP =
{ 'debug' => ::Logger::DEBUG, 'info' => ::Logger::INFO, 'warn' => ::Logger::WARN, 'error' => ::Logger::ERROR, 'fatal' => ::Logger::FATAL, 'unknown' => ::Logger::UNKNOWN, }.freeze
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.
-
.logger ⇒ MultiLogger
Exposes a logger instance.
-
.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.
- .new_logger(*logdevs, **configs) ⇒ Object
- .verbose? ⇒ Boolean
Instance Method Summary collapse
-
#current_log_config ⇒ Object
Exposes the current log config.
- #current_log_format ⇒ Object
-
#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_formatter(f) ⇒ Object
-
#new_log_level(level) ⇒ Object
Exposes setting the log level.
- #new_logger(*logdevs, **configs) ⇒ Object
- #verbose? ⇒ Boolean
Class Method Details
.current_log_config ⇒ Hash
Returns the current log config if set, or the default if not.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/cem_acpt/logging.rb', line 270 def current_log_config return @log_config if @log_config @log_config = { logdev: $stdout, shift_age: 0, shift_size: 1_048_576, level: ::Logger::INFO, progname: 'CemAcpt', datetime_format: '%Y%m%dT%H%M%S%z', formatter: current_log_format, } @log_config end |
.current_log_format ⇒ Symbol
Shows the current log format style if set, or the default if not.
256 257 258 |
# File 'lib/cem_acpt/logging.rb', line 256 def current_log_format @current_log_format ||= :text end |
.current_log_level ⇒ Logger::Severity
Shortcut method for logger.level
238 239 240 |
# File 'lib/cem_acpt/logging.rb', line 238 def current_log_level logger.level end |
.included(base) ⇒ Object
Provides class method wrappers for logging methods
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/cem_acpt/logging.rb', line 325 def self.included(base) class << base def new_logger(*logdevs, **configs) CemAcpt::Logging.new_logger(*logdevs, **configs) end 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 verbose? CemAcpt::Logging.verbose? end def current_log_format CemAcpt::Logging.current_log_format end def new_log_formatter(f) CemAcpt::Logging.new_log_formatter(f) 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 |
.logger ⇒ MultiLogger
Exposes a logger instance. Will either use the currently set logger or create a new one.
232 233 234 |
# File 'lib/cem_acpt/logging.rb', line 232 def logger @logger ||= new_logger($stdout) 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.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/cem_acpt/logging.rb', line 296 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 = MultiLogger.new(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=
264 265 266 |
# File 'lib/cem_acpt/logging.rb', line 264 def new_log_formatter(f) CemAcpt::Logging::Formatter.for(f) end |
.new_log_level(level) ⇒ Object
Shortcut method to set logger.level
244 245 246 247 248 |
# File 'lib/cem_acpt/logging.rb', line 244 def new_log_level(level) raise ArgumentError, 'Log level not recognized' unless LEVEL_MAP[level.downcase] @logger.level = LEVEL_MAP[level.downcase] end |
.new_logger(*logdevs, **configs) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/cem_acpt/logging.rb', line 210 def new_logger(*logdevs, **configs) new_configs = current_log_config.merge(configs.reject { |_, v| v.nil? }) if new_configs.key?(:level) && !LEVEL_MAP.values.include?(new_configs[:level]) new_configs[:level] = LEVEL_MAP[new_configs[:level].downcase] end loggers = logdevs.map do |dev| logdev = dev.is_a?(String) ? $stdout : dev logger = Logger.new( logdev, new_configs[:shift_age], new_configs[:shift_size], **new_configs.reject { |k, _| %i[logdev shift_age shift_size].include?(k) }, ) logger.reopen(dev) if dev.is_a?(String) logger end @logger = CemAcpt::Logging::MultiLogger.new(*loggers) end |
.verbose? ⇒ Boolean
250 251 252 |
# File 'lib/cem_acpt/logging.rb', line 250 def verbose? logger.is_a?(CemAcpt::Logging::MultiLogger) ? logger.verbose?.all? : logger.verbose? end |
Instance Method Details
#current_log_config ⇒ Object
Exposes the current log config
397 398 399 |
# File 'lib/cem_acpt/logging.rb', line 397 def current_log_config CemAcpt::Logging.current_log_config end |
#current_log_format ⇒ Object
388 389 390 |
# File 'lib/cem_acpt/logging.rb', line 388 def current_log_format CemAcpt::Logging.current_log_format end |
#current_log_level ⇒ Object
Exposes the current log level
375 376 377 |
# File 'lib/cem_acpt/logging.rb', line 375 def current_log_level CemAcpt::Logging.current_log_level end |
#logger ⇒ Object
Exposes the logger instance
370 371 372 |
# File 'lib/cem_acpt/logging.rb', line 370 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
402 403 404 |
# File 'lib/cem_acpt/logging.rb', line 402 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_formatter(f) ⇒ Object
392 393 394 |
# File 'lib/cem_acpt/logging.rb', line 392 def new_log_formatter(f) CemAcpt::Logging.new_log_formatter(f) end |
#new_log_level(level) ⇒ Object
Exposes setting the log level
380 381 382 |
# File 'lib/cem_acpt/logging.rb', line 380 def new_log_level(level) CemAcpt::Logging.new_log_level(level) end |
#new_logger(*logdevs, **configs) ⇒ Object
365 366 367 |
# File 'lib/cem_acpt/logging.rb', line 365 def new_logger(*logdevs, **configs) CemAcpt::Logging.new_logger(*logdevs, **configs) end |