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,
}

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cem_acpt/logging.rb', line 249

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



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

def current_log_format
  @current_log_format ||= :text
end

.current_log_levelLogger::Severity

Shortcut method for logger.level

Returns:

  • (Logger::Severity)


221
222
223
# File 'lib/cem_acpt/logging.rb', line 221

def current_log_level
  logger.level
end

.included(base) ⇒ Object

Provides class method wrappers for logging methods



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/cem_acpt/logging.rb', line 304

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:



215
216
217
# File 'lib/cem_acpt/logging.rb', line 215

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



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/cem_acpt/logging.rb', line 275

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=



243
244
245
# File 'lib/cem_acpt/logging.rb', line 243

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)


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

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cem_acpt/logging.rb', line 193

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



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

def current_log_config
  CemAcpt::Logging.current_log_config
end

#current_log_formatObject



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

def current_log_format
  CemAcpt::Logging.current_log_format
end

#current_log_levelObject

Exposes the current log level



350
351
352
# File 'lib/cem_acpt/logging.rb', line 350

def current_log_level
  CemAcpt::Logging.current_log_level
end

#loggerObject

Exposes the logger instance



345
346
347
# File 'lib/cem_acpt/logging.rb', line 345

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



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

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



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

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

#new_log_level(level) ⇒ Object

Exposes setting the log level



355
356
357
# File 'lib/cem_acpt/logging.rb', line 355

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

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



340
341
342
# File 'lib/cem_acpt/logging.rb', line 340

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