Sha256: 6bb69f9eb32562ce551fa4edafdd125283a84b499a6991dcbe805d73e513400b

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
require 'fedux_org_stdlib/require_files'
require_library %w(logger forwardable)

require 'fedux_org_stdlib/logging/severity'

module FeduxOrgStdlib
  module Logging
    # The logger
    class Logger
      extend ::Forwardable
      def_delegators :@logger, :fatal, :warn, :debug, :info, :error, :unknown

      attr_reader :mode

      # Create logger
      #
      # @param [Logger] logger (::Logger.new( $stderr ))
      #   The logger with should be used to output data
      def initialize(logger = ::Logger.new($stderr))
        @logger = logger
        self.mode = :info
      end

      # Reset logger to ruby logger
      def reset
        @logger = ::Logger.new($stderr)
      end

      # Change mode of logger: DEBUG, ... + Change the output format
      #
      # @param [Symbol] m
      #   the mode: DEBUG, ...
      def mode=(m)
        @mode = m

        case m
        when :debug
          @logger.level = ::Logger::DEBUG
          format_debug
        when :silent
          @logger.level = ::Logger::SILENT
        when :info
          @logger.level = ::Logger::INFO
          format_standard
        else
          @logger.level = ::Logger::INFO
          format_standard
        end
      end

      private

      def format_debug
        @logger.formatter = proc { |severity, datetime, progname, msg|
          debug_msg(format("%s %s %s: %s\n", datetime, severity, progname, msg))
        }
      end

      def format_standard
        @logger.formatter = proc { |severity, datetime, _, msg|
          info_msg(format("%s %s: %s\n", datetime, severity, msg))
        }
      end

      def error_msg(msg)
        ANSI.red(msg)
      end

      def info_msg(msg)
        msg
      end

      def debug_msg(msg)
        msg
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.7.22 lib/fedux_org_stdlib/logging/logger.rb