Sha256: 4a6e9b726984fa4f867d55157b3002fbb20c45869bffac1a164fc05ea7e85b03

Contents?: true

Size: 1.02 KB

Versions: 30

Compression:

Stored size: 1.02 KB

Contents

require 'logger'

require 'colored'

module Pupa
  # A logger factory.
  class Logger
    # Returns a configured logger.
    #
    # @param [String] progname the name of the program performing the logging
    # @param [String] level the log level, one of "DEBUG", "INFO", "WARN",
    #   "ERROR", "FATAL" or "UNKNOWN"
    # @param [String,IO] logdev the log device
    # @return [Logger] a configured logger
    def self.new(progname, level: 'INFO', logdev: STDOUT)
      logger = ::Logger.new(logdev)
      logger.level = ::Logger.const_get(level)
      logger.progname = progname
      logger.formatter = proc do |severity, datetime, progname, msg|
        message = "#{datetime.strftime('%T')} #{severity} #{progname}: #{msg}\n"
        case severity
        when 'DEBUG'
          message.magenta
        when 'INFO'
          message.white
        when 'WARN'
          message.yellow
        when 'ERROR'
          message.red
        when 'FATAL'
          message.bold.red_on_white
        end
      end
      logger
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
pupa-0.0.10 lib/pupa/logger.rb
pupa-0.0.9 lib/pupa/logger.rb
pupa-0.0.8 lib/pupa/logger.rb
pupa-0.0.7 lib/pupa/logger.rb
pupa-0.0.6 lib/pupa/logger.rb
pupa-0.0.5 lib/pupa/logger.rb
pupa-0.0.4 lib/pupa/logger.rb
pupa-0.0.3 lib/pupa/logger.rb
pupa-0.0.2 lib/pupa/logger.rb
pupa-0.0.1 lib/pupa/logger.rb