Module: CemAcpt::Logging

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

Instance Method Summary collapse

Class Method Details

.current_log_configHash

Returns the current log config if set, or the default if not.

Returns:

  • (Hash)

    the current log config



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_formatSymbol

Shows the current log format style if set, or the default if not.

Returns:

  • (Symbol)

    the current log format style



256
257
258
# File 'lib/cem_acpt/logging.rb', line 256

def current_log_format
  @current_log_format ||= :text
end

.current_log_levelLogger::Severity

Shortcut method for logger.level

Returns:

  • (Logger::Severity)


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

.loggerMultiLogger

Exposes a logger instance. Will either use the currently set logger or create a new one.

Returns:



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.

Parameters:

  • logdev (String, IO) (defaults to: nil)

    the log device to use. If ‘stdout’ or ‘stderr’ are passed, the respective IO object will be used. Otherwise, Strings will be treated as file paths. If an IO object is passed, it will be used directly. If no logdev is passed, or an invalid logdev is passed, the default is $stdout.

  • shift_age (String) (defaults to: nil)

    the log rotation age

  • shift_size (Integer) (defaults to: nil)

    the log rotation size

  • level (Logger::Severity) (defaults to: nil)

    the log level

  • formatter (Proc) (defaults to: nil)

    the log formatter

  • datetime_format (String) (defaults to: nil)

    the datetime format



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=

Parameters:

  • f (Symbol)

    the log format style to set

Returns:

  • (Proc)

    the 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

Parameters:

  • level (String)

    the log level to set

Raises:

  • (ArgumentError)


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

Returns:

  • (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_configObject

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_formatObject



388
389
390
# File 'lib/cem_acpt/logging.rb', line 388

def current_log_format
  CemAcpt::Logging.current_log_format
end

#current_log_levelObject

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

#loggerObject

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

#verbose?Boolean

Returns:

  • (Boolean)


384
385
386
# File 'lib/cem_acpt/logging.rb', line 384

def verbose?
  CemAcpt::Logging.verbose?
end