Sha256: 8d6b1b69766ed6732665d728618c2e515078d094bf5993b865d1e4084cf66462

Contents?: true

Size: 1.86 KB

Versions: 44

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "active_support/concern"
require "active_support/core_ext/module/attribute_accessors"
require "concurrent"
require "fiber"

module ActiveSupport
  module LoggerThreadSafeLevel # :nodoc:
    extend ActiveSupport::Concern

    Logger::Severity.constants.each do |severity|
      class_eval(<<-EOT, __FILE__, __LINE__ + 1)
        def #{severity.downcase}?                # def debug?
          Logger::#{severity} >= level           #   DEBUG >= level
        end                                      # end
      EOT
    end

    def local_level
      IsolatedExecutionState[:logger_thread_safe_level]
    end

    def local_level=(level)
      case level
      when Integer
      when Symbol
        level = Logger::Severity.const_get(level.to_s.upcase)
      when nil
      else
        raise ArgumentError, "Invalid log level: #{level.inspect}"
      end
      IsolatedExecutionState[:logger_thread_safe_level] = level
    end

    def level
      local_level || super
    end

    # Change the thread-local level for the duration of the given block.
    def log_at(level)
      old_local_level, self.local_level = local_level, level
      yield
    ensure
      self.local_level = old_local_level
    end

    # Redefined to check severity against #level, and thus the thread-local level, rather than +@level+.
    # FIXME: Remove when the minimum Ruby version supports overriding Logger#level.
    def add(severity, message = nil, progname = nil, &block) # :nodoc:
      severity ||= UNKNOWN
      progname ||= @progname

      return true if @logdev.nil? || severity < level

      if message.nil?
        if block_given?
          message  = yield
        else
          message  = progname
          progname = @progname
        end
      end

      @logdev.write \
        format_message(format_severity(severity), Time.now, progname, message)
    end
  end
end

Version data entries

44 entries across 41 versions & 8 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.8.5 lib/active_support/logger_thread_safe_level.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/logger_thread_safe_level.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/logger_thread_safe_level.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/logger_thread_safe_level.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.8.4 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.8.1 lib/active_support/logger_thread_safe_level.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/logger_thread_safe_level.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.8 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.7.2 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.7.1 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.7 lib/active_support/logger_thread_safe_level.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/logger_thread_safe_level.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/logger_thread_safe_level.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.6 lib/active_support/logger_thread_safe_level.rb
activesupport-7.0.5.1 lib/active_support/logger_thread_safe_level.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/logger_thread_safe_level.rb