Sha256: 5794b4ca1c71133c028d171ecc149dfb54f7e8e36fee3997e0971f30dd624d03

Contents?: true

Size: 1.95 KB

Versions: 18

Compression:

Stored size: 1.95 KB

Contents

module Eco
  module Language
    # Basic logger to format messaging to console
    class BasicLogger
      class << self
        def forward(*meths)
          meths.each do |meth|
            define_method(meth) do |*args, **kargs, &block|
              forward(meth, *args, **kargs, &block)
            end
          end
        end
      end

      TIMESTAMP_PATTERN = '%Y-%m-%dT%H:%M:%S'.freeze
      LOG_LEVELS = %i[unknown fatal error warn info debug].freeze
      METHODS    = %i[<< close add].freeze

      attr_writer :timestamp
      attr_reader :level

      forward *LOG_LEVELS, *METHODS

      def initialize(level: ::Logger::INFO, timestamp: false)
        @level             = level
        self.timestamp     = timestamp
        loggers[:console]  = ::Logger.new(STDOUT).tap do |logger|
          logger.formatter = format_proc(console: true)
          logger.level     = level
        end
      end

      def timestamp?
        @timestamp
      end

      def level=(value)
        loggers[:console].level = value
      end

      private

      def forward(meth, *args, &block)
        loggers.each do |_key, logger|
          logger.send(meth, *args, &block)
        end
      end

      def loggers
        @loggers ||= {}
      end

      def console_timestamp(datetime)
        return nil unless timestamp?
        timestamp(datetime)
      end

      def timestamp(datetime)
        return nil unless datetime
        str_date = datetime.strftime(self.class::TIMESTAMP_PATTERN)
        "#{str_date} > "
      end

      def format_proc(console: true, &block)
        proc do |severity, datetime, progname, msg|
          str_stamp     = console ? console_timestamp(datetime) : timestamp(datetime)
          "#{severity.to_s[0]}: #{str_stamp}#{msg}\n".tap do |formatted_msg|
            block.call(severity, datetime, msg, formatted_msg) if block
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
eco-helpers-2.7.12 lib/eco/language/basic_logger.rb
eco-helpers-2.7.4 lib/eco/language/basic_logger.rb
eco-helpers-2.7.2 lib/eco/language/basic_logger.rb
eco-helpers-2.7.1 lib/eco/language/basic_logger.rb
eco-helpers-2.7.0 lib/eco/language/basic_logger.rb
eco-helpers-2.6.4 lib/eco/language/basic_logger.rb
eco-helpers-2.6.3 lib/eco/language/basic_logger.rb
eco-helpers-2.6.2 lib/eco/language/basic_logger.rb
eco-helpers-2.6.1 lib/eco/language/basic_logger.rb
eco-helpers-2.6.0 lib/eco/language/basic_logger.rb
eco-helpers-2.5.10 lib/eco/language/basic_logger.rb
eco-helpers-2.5.9 lib/eco/language/basic_logger.rb
eco-helpers-2.5.8 lib/eco/language/basic_logger.rb
eco-helpers-2.5.7 lib/eco/language/basic_logger.rb
eco-helpers-2.5.6 lib/eco/language/basic_logger.rb
eco-helpers-2.5.5 lib/eco/language/basic_logger.rb
eco-helpers-2.5.4 lib/eco/language/basic_logger.rb
eco-helpers-2.5.3 lib/eco/language/basic_logger.rb