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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/cem_acpt/logging.rb', line 262

def current_log_config
  return @log_config if @log_config

  @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: 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



248
249
250
# File 'lib/cem_acpt/logging.rb', line 248

def current_log_format
  @current_log_format ||= :text
end

.current_log_levelLogger::Severity

Shortcut method for logger.level

Returns:

  • (Logger::Severity)


234
235
236
# File 'lib/cem_acpt/logging.rb', line 234

def current_log_level
  logger.level
end

.included(base) ⇒ Object

Provides class method wrappers for logging methods



317
318
319
320
321
322
323
324
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
# File 'lib/cem_acpt/logging.rb', line 317

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 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:



228
229
230
# File 'lib/cem_acpt/logging.rb', line 228

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cem_acpt/logging.rb', line 288

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=



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

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)


240
241
242
243
244
# File 'lib/cem_acpt/logging.rb', line 240

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cem_acpt/logging.rb', line 206

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

Instance Method Details

#current_log_configObject

Exposes the current log config



381
382
383
# File 'lib/cem_acpt/logging.rb', line 381

def current_log_config
  CemAcpt::Logging.current_log_config
end

#current_log_formatObject



372
373
374
# File 'lib/cem_acpt/logging.rb', line 372

def current_log_format
  CemAcpt::Logging.current_log_format
end

#current_log_levelObject

Exposes the current log level



363
364
365
# File 'lib/cem_acpt/logging.rb', line 363

def current_log_level
  CemAcpt::Logging.current_log_level
end

#loggerObject

Exposes the logger instance



358
359
360
# File 'lib/cem_acpt/logging.rb', line 358

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



386
387
388
# File 'lib/cem_acpt/logging.rb', line 386

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



376
377
378
# File 'lib/cem_acpt/logging.rb', line 376

def new_log_formatter(f)
  CemAcpt::Logging.new_log_formatter(f)
end

#new_log_level(level) ⇒ Object

Exposes setting the log level



368
369
370
# File 'lib/cem_acpt/logging.rb', line 368

def new_log_level(level)
  CemAcpt::Logging.new_log_level(level)
end

#new_logger(*logdevs, **configs) ⇒ Object



353
354
355
# File 'lib/cem_acpt/logging.rb', line 353

def new_logger(*logdevs, **configs)
  CemAcpt::Logging.new_logger(*logdevs, **configs)
end