Sha256: 5540c9a7a96278ce4bec08ab069086aa2ba7ed2c4bc2abfb23116caa7265f7e2

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

require 'logger'
require 'forwardable'
require 'fedux_org/stdlib/logging/severity'

module FeduxOrg
  module Stdlib
    module Logging
      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("%s %s %s: %s\n" % [ datetime, severity, progname, msg ])
          }
        end

        def format_standard
          @logger.formatter = proc { |severity, datetime, _, msg|
            info_msg( "%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
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.3.2 lib/fedux_org/stdlib/logging/logger.rb
fedux_org-stdlib-0.3.0 lib/fedux_org/stdlib/logging/logger.rb
fedux_org-stdlib-0.2.1 lib/fedux_org/stdlib/logging/logger.rb
fedux_org-stdlib-0.1.3 lib/fedux_org/stdlib/logging/logger.rb
fedux_org-stdlib-0.1.2 lib/fedux_org/stdlib/logging/logger.rb
fedux_org-stdlib-0.1.1 lib/fedux_org/stdlib/logging/logger.rb