Module: ActiveSupport::LoggerThreadSafeLevel

Extended by:
Concern
Defined in:
activesupport/lib/active_support/logger_thread_safe_level.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Concern

append_features, class_methods, extended, included, prepend_features, prepended

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object

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.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 66

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

#after_initializeObject


24
25
26
27
28
29
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 24

def after_initialize
  ActiveSupport::Deprecation.warn(
    "Logger don't need to call #after_initialize directly anymore. It will be deprecated without replacement in " \
    "Rails 6.1."
  )
end

#levelObject


52
53
54
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 52

def level
  local_level || super
end

#local_levelObject


35
36
37
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 35

def local_level
  self.class.local_levels[local_log_id]
end

#local_level=(level) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 39

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

#local_log_idObject


31
32
33
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 31

def local_log_id
  Fiber.current.__id__
end

#log_at(level) ⇒ Object

Change the thread-local level for the duration of the given block.


57
58
59
60
61
62
# File 'activesupport/lib/active_support/logger_thread_safe_level.rb', line 57

def log_at(level)
  old_local_level, self.local_level = local_level, level
  yield
ensure
  self.local_level = old_local_level
end