Sha256: 4ace2d5bcd184840ad3274a1bdd08bd9392ed483e4628024c490accb8f214450

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'logger'

module Stove
  module Logger
    class << self
      def set_level(level)
        logger.level = level_to_constant(level)
        logger
      end

      def set_output(output)
        old_level = @logger.sev_threshold

        @logger = ::Logger.new(output)
        @logger.level = old_level
        @logger
      end

      [:fatal, :error, :warn, :info, :debug, :sev_threshold].each do |name|
        define_method(name) do |*args|
          logger.send(name, *args)
        end
      end

      private
        def logger
          @logger ||= begin
            logger = ::Logger.new($stdout)
            logger.level = ::Logger::WARN
            logger
          end
        end

        # Convert a string to it's logger constant.
        #
        # @return [Object]
        def level_to_constant(level)
          return level if level.kind_of?(Fixnum)
          case level.to_s.strip.downcase.to_sym
            when :fatal
              ::Logger::FATAL
            when :error
              ::Logger::ERROR
            when :warn
              ::Logger::WARN
            when :info
              ::Logger::INFO
            when :debug
              ::Logger::DEBUG
            else
              ::Logger::INFO
            end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stove-1.1.2 lib/stove/logger.rb
stove-1.1.0 lib/stove/logger.rb
stove-1.0.1 lib/stove/logger.rb