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



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

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



237
238
239
# File 'lib/cem_acpt/logging.rb', line 237

def current_log_format
  @current_log_format ||= :text
end

.current_log_levelLogger::Severity

Shortcut method for logger.level

Returns:

  • (Logger::Severity)


223
224
225
# File 'lib/cem_acpt/logging.rb', line 223

def current_log_level
  logger.level
end

.included(base) ⇒ Object

Provides class method wrappers for logging methods



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
339
340
# File 'lib/cem_acpt/logging.rb', line 306

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:



217
218
219
# File 'lib/cem_acpt/logging.rb', line 217

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



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

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=



245
246
247
# File 'lib/cem_acpt/logging.rb', line 245

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)


229
230
231
232
233
# File 'lib/cem_acpt/logging.rb', line 229

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



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

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



370
371
372
# File 'lib/cem_acpt/logging.rb', line 370

def current_log_config
  CemAcpt::Logging.current_log_config
end

#current_log_formatObject



361
362
363
# File 'lib/cem_acpt/logging.rb', line 361

def current_log_format
  CemAcpt::Logging.current_log_format
end

#current_log_levelObject

Exposes the current log level



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

def current_log_level
  CemAcpt::Logging.current_log_level
end

#loggerObject

Exposes the logger instance



347
348
349
# File 'lib/cem_acpt/logging.rb', line 347

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



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

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



365
366
367
# File 'lib/cem_acpt/logging.rb', line 365

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

#new_log_level(level) ⇒ Object

Exposes setting the log level



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

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

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



342
343
344
# File 'lib/cem_acpt/logging.rb', line 342

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