module Nucleus::Logging

Logging module for Nucleus. Include via

include Nucleus::Logging

and then log your messages:

log.info('This is a test log message')

@author Willem Buys Idea by Willem ‘Jacob’ Buys, as seen on stackoverflow.com/questions/917566/ruby-share-logger-instance-among-module-classes

Public Class Methods

configure_logger_for(classname) click to toggle source
# File lib/nucleus/core/common/logging/logging.rb, line 23
def configure_logger_for(classname)
  # prepare logging dir
  log_dir = nucleus_config.logging.path
  log_file = File.join(log_dir, 'nucleus.log')
  # prepare path and create missing directories
  FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir)
  # create the loggers
  std_log = Logger.new(STDOUT)
  # use rotation for x days
  file_log = Logger.new(log_file, 'daily', 7)

  # include custom log format that includes the request id
  formatter = Nucleus::Logging::Formatter.new

  [file_log, std_log].each do |logger|
    # apply format
    logger.formatter = formatter
    # apply the classname
    logger.progname = classname
  end

  # apply the log level from the app. configuration
  multi_logger = MultiLogger.new(
    level: nucleus_config.logging.key?(:level) ? nucleus_config.logging.level : Logger::Severity::WARN,
    loggers: [std_log, file_log])
  multi_logger
end
logger_for(classname) click to toggle source
# File lib/nucleus/core/common/logging/logging.rb, line 19
def logger_for(classname)
  @loggers[classname] ||= configure_logger_for(classname)
end

Public Instance Methods

log() click to toggle source
# File lib/nucleus/core/common/logging/logging.rb, line 11
def log
  @log ||= Logging.logger_for(self.class.name)
end