Module: Nucleus::Logging

Overview

Logging module for Nucleus. Include via

include Nucleus::Logging

and then log your messages:

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

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

Author:

  • Willem Buys

Defined Under Namespace

Classes: Formatter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_logger_for(classname) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



19
20
21
# File 'lib/nucleus/core/common/logging/logging.rb', line 19

def logger_for(classname)
  @loggers[classname] ||= configure_logger_for(classname)
end

Instance Method Details

#logObject



11
12
13
# File 'lib/nucleus/core/common/logging/logging.rb', line 11

def log
  @log ||= Logging.logger_for(self.class.name)
end