Sha256: cf136b3afe0acbf4b1fb251ea690c82d95b0442b5ee44a8727fab59a4ab5e41c

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Zapp
  # The default logger for zap
  class Logger
    # Base contains all the logging functionality and is included both as class and instance methods of Zap::Logger
    # This allows logging without creating new instances,
    # while allowing Ractors to create their own instances for thread safety
    module Base
      attr_writer(:level, :prefix)

      LEVELS = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3 }.freeze

      def debug(msg)
        log("DEBUG", msg)
      end

      def info(msg)
        log("INFO", msg)
      end

      def warn(msg)
        log("WARN", msg)
      end

      def error(msg)
        log("ERROR", msg)
      end

      def level
        @level ||= if ENV["ZAPP_LOG_LEVEL"] != "" && !ENV["ZAPP_LOG_LEVEL"].nil?
                     if LEVELS[ENV["ZAPP_LOG_LEVEL"]].nil?
                       raise(
                         Zapp::ZappError,
                         "Invalid log level '#{ENV['ZAP_LOG_LEVEL']}', must be one of [#{LEVELS.keys.join(', ')}]"
                       )
                     else
                       LEVELS[ENV["ZAP_LOG_LEVEL"]]
                     end
                   else
                     LEVELS[:DEBUG]
                   end
      end

      private

      def log(current_level, msg)
        puts("--- #{@prefix} [#{current_level}] #{msg}") if level <= LEVELS[current_level.to_sym]
      end
    end
    include(Zapp::Logger::Base)

    def initialize
      @prefix = "Zap"
      yield(self) if block_given?
    end

    class << self
      include(Zapp::Logger::Base)
      @prefix = "Zap"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zapp-0.1.1 lib/zapp/logger.rb
zapp-0.1.0 lib/zapp/logger.rb