Sha256: 773e5479ec4194a5f9db0965d90edc020e627d218768f19c66c748629d0fa51f

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  class Logger < ::Logger
    # The line below is handled by activesupport-logger.rb
    # include AltLoggerSilencePart2 (& Part1 can't be used)

    # Returns true if the logger destination matches one of the sources
    #
    #   logger = Logger.new(STDOUT)
    #   ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)
    #   # => true
    #
    #   logger = Logger.new('/var/log/rails.log')
    #   ActiveSupport::Logger.logger_outputs_to?(logger, '/var/log/rails.log')
    #   # => true
    def self.logger_outputs_to?(logger, *sources)
      loggers = if logger.is_a?(BroadcastLogger)
        logger.broadcasts
      else
        [logger]
      end

      logdevs = loggers.map { |logger| logger.instance_variable_get(:@logdev) }
      logger_sources = logdevs.filter_map { |logdev| logdev.try(:filename) || logdev.try(:dev) }

      normalize_sources(sources).intersect?(normalize_sources(logger_sources))
    end

    def initialize(*args, **kwargs)
      super
      @formatter ||= SimpleFormatter.new
    end

    # Simple formatter which only displays the message.
    class SimpleFormatter < ::Logger::Formatter
      # This method is invoked when a log event occurs
      def call(severity, timestamp, progname, msg)
        "#{(String === msg) ? msg : msg.inspect}\n"
      end
    end

    private

    def self.normalize_sources(sources)
      sources.map do |source|
        source = source.path if source.respond_to?(:path)
        source = File.realpath(source) if source.is_a?(String) && File.exist?(source)
        source
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activesupport-logger-1.0.1 lib/activesupport/logger.rb
activesupport-logger-1.0.0 lib/activesupport/logger.rb