Module: Sidekiq::LoggingUtils

Included in:
Logger
Defined in:
lib/sidekiq/logger.rb

Constant Summary collapse

LEVELS =
{
  "debug" => 0,
  "info" => 1,
  "warn" => 2,
  "error" => 3,
  "fatal" => 4,
}

Instance Method Summary collapse

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sidekiq/logger.rb', line 85

def add(severity, message = nil, progname = nil, &block)
  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

#debug?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/sidekiq/logger.rb', line 33

def debug?
  level >= 0
end

#error?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/sidekiq/logger.rb', line 45

def error?
  level >= 3
end

#fatal?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/sidekiq/logger.rb', line 49

def fatal?
  level >= 4
end

#info?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sidekiq/logger.rb', line 37

def info?
  level >= 1
end

#levelObject



70
71
72
# File 'lib/sidekiq/logger.rb', line 70

def level
  local_level || super
end

#local_levelObject



53
54
55
# File 'lib/sidekiq/logger.rb', line 53

def local_level
  Thread.current[:sidekiq_log_level]
end

#local_level=(level) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sidekiq/logger.rb', line 57

def local_level=(level)
  case level
  when Integer
    Thread.current[:sidekiq_log_level] = level
  when Symbol, String
    Thread.current[:sidekiq_log_level] = LEVELS[level.to_s]
  when nil
    Thread.current[:sidekiq_log_level] = nil
  else
    raise ArgumentError, "Invalid log level: #{level.inspect}"
  end
end

#log_at(level) ⇒ Object

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



75
76
77
78
79
80
81
# File 'lib/sidekiq/logger.rb', line 75

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

#warn?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sidekiq/logger.rb', line 41

def warn?
  level >= 2
end