Sha256: 9c60d2e6106306243c2125fa7c7ebe4299d4d8e774a5f1df8a7b44db83180f9b

Contents?: true

Size: 1.7 KB

Versions: 127

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Karafka
  module Instrumentation
    # Default logger for Event Delegator
    # @note It uses ::Logger features - providing basic logging
    class Logger < ::Logger
      # Map containing information about log level for given environment
      ENV_MAP = {
        'production' => ::Logger::ERROR,
        'test' => ::Logger::ERROR,
        'development' => ::Logger::INFO,
        'debug' => ::Logger::DEBUG,
        'default' => ::Logger::INFO
      }.freeze

      private_constant :ENV_MAP

      # Creates a new instance of logger ensuring that it has a place to write to
      # @param _args Any arguments that we don't care about but that are needed in order to
      #   make this logger compatible with the default Ruby one
      def initialize(*_args)
        super(target)
        self.level = ENV_MAP[Karafka.env] || ENV_MAP['default']
      end

      private

      # @return [Karafka::Helpers::MultiDelegator] multi delegator instance
      #   to which we will be writing logs
      # We use this approach to log stuff to file and to the $stdout at the same time
      def target
        Karafka::Helpers::MultiDelegator
          .delegate(:write, :close)
          .to(*[$stdout, file].compact)
      end

      # @return [Pathname] Path to a file to which we should log
      def log_path
        @log_path ||= Karafka::App.root.join("log/#{Karafka.env}.log")
      end

      # @return [File] file to which we want to write our logs
      # @note File is being opened in append mode ('a')
      def file
        FileUtils.mkdir_p(File.dirname(log_path))

        @file ||= File.open(log_path, 'a')
      rescue Errno::EACCES, Errno::EROFS
        nil
      end
    end
  end
end

Version data entries

127 entries across 127 versions & 1 rubygems

Version Path
karafka-2.4.17 lib/karafka/instrumentation/logger.rb
karafka-2.4.16 lib/karafka/instrumentation/logger.rb
karafka-2.4.15 lib/karafka/instrumentation/logger.rb
karafka-2.4.14 lib/karafka/instrumentation/logger.rb
karafka-2.4.13 lib/karafka/instrumentation/logger.rb
karafka-2.4.12 lib/karafka/instrumentation/logger.rb
karafka-2.4.11 lib/karafka/instrumentation/logger.rb
karafka-2.4.10 lib/karafka/instrumentation/logger.rb
karafka-2.4.9 lib/karafka/instrumentation/logger.rb
karafka-2.4.8 lib/karafka/instrumentation/logger.rb
karafka-2.4.7 lib/karafka/instrumentation/logger.rb
karafka-2.4.6 lib/karafka/instrumentation/logger.rb
karafka-2.4.5 lib/karafka/instrumentation/logger.rb
karafka-2.4.4 lib/karafka/instrumentation/logger.rb
karafka-2.4.3 lib/karafka/instrumentation/logger.rb
karafka-2.4.0 lib/karafka/instrumentation/logger.rb
karafka-2.4.0.rc1 lib/karafka/instrumentation/logger.rb
karafka-2.3.4 lib/karafka/instrumentation/logger.rb
karafka-2.4.0.beta2 lib/karafka/instrumentation/logger.rb
karafka-2.4.0.beta1 lib/karafka/instrumentation/logger.rb