module CliUI::Loggable

Handle application log

Attributes

level[RW]

@!attribute [rw] level

Log output level. Can be one of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR

@!attribute [rw] log_path

Path in which log files are saved ( default STDOUT )
log_path[RW]

@!attribute [rw] level

Log output level. Can be one of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR

@!attribute [rw] log_path

Path in which log files are saved ( default STDOUT )

Public Class Methods

configure_logger_for(classname) click to toggle source

Create and configure a logger for the specified Class

@param classname [String] the name of the class for which a logger instance must be retrieved @return [Object] the instance of the logger class for the specified Class

# File lib/cli_ui/loggable.rb, line 48
def configure_logger_for(classname)
  # handle case in which log path does not exists
  begin
    logger = Logger.new(@log_path)
  rescue Errno::ENOENT
    FileUtils.mkdir_p File.dirname @log_path
    retry
  end

  logger.progname = classname
  logger.level = @level
  logger.formatter = proc { |severity, datetime, progname, msg|
    case severity
    when 'DEBUG'
      spaciator = "    *"
      after_space = ""
    when 'INFO'
      spaciator = "   **"
      after_space = " "
    when 'WARN'
      spaciator = "  ***"
      after_space = " "
    when 'ERROR'
      spaciator = " ****"
      after_space = ""
    when 'FATAL'
      spaciator = "*****"
      after_space = ""
    else
      spaciator = "     "
      after_space = ""
    end

    formatted_output = " #{spaciator} [#{severity}]#{after_space} [#{datetime}] -- #{msg} { #{progname} }\n"
    formatted_output
  }
  logger
end
logger_for(classname) click to toggle source

Return the logger for a specific Class. If the instance is not found, creates it.

@param classname [String] the name of the class for which a logger instance must be retrieved @return [Object] the instance of the logger class for the specified Class

# File lib/cli_ui/loggable.rb, line 40
def logger_for(classname)
  @loggers[classname] ||= configure_logger_for(classname)
end

Public Instance Methods

logger() click to toggle source

Global, memoized, lazy initialized instance of a logger

This is the magical bit that gets mixed into your classes. Respond to Logger function.

@return [Object] an instance of the logger class

# File lib/cli_ui/loggable.rb, line 24
def logger
  classname = (self.is_a? Module) ? self : self.class.name
  @logger ||= Loggable.logger_for(classname)
end