Module: ActiveSupport::LoggerThreadSafeLevel

Extended by:
Concern
Defined in:
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.


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_support/logger_thread_safe_level.rb', line 59

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

#levelObject


45
46
47
# File 'lib/active_support/logger_thread_safe_level.rb', line 45

def level
  local_level || super
end

#local_levelObject


28
29
30
# File 'lib/active_support/logger_thread_safe_level.rb', line 28

def local_level
  self.class.local_levels[local_log_id]
end

#local_level=(level) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_support/logger_thread_safe_level.rb', line 32

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


24
25
26
# File 'lib/active_support/logger_thread_safe_level.rb', line 24

def local_log_id
  Fiber.current.__id__
end

#log_at(level) ⇒ Object

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


50
51
52
53
54
55
# File 'lib/active_support/logger_thread_safe_level.rb', line 50

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