Module: Thin::Logging

Included in:
Command, Connection, Controllers::Controller, Server
Defined in:
lib/thin/logging.rb

Overview

To be included in classes to allow some basic logging that can be silenced (Logging.silent=) or made more verbose. Logging.trace=: log all raw request and response and

messages logged with +trace+.

Logging.silent=: silence all log all log messages

altogether.

Defined Under Namespace

Classes: SimpleFormatter

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/thin/logging.rb', line 23

def logger
  @logger
end

.trace_loggerObject

Returns the value of attribute trace_logger.



24
25
26
# File 'lib/thin/logging.rb', line 24

def trace_logger
  @trace_logger
end

Class Method Details

.debug=(val) ⇒ Object



115
116
117
# File 'lib/thin/logging.rb', line 115

def debug=(val)
  self.level = (val ? Logger::DEBUG : Logger::INFO)
end

.debug?Boolean

Provided for backwards compatibility. Callers should be using the level (on the Logging module or on the instance) to figure out what the log level is.



112
113
114
# File 'lib/thin/logging.rb', line 112

def debug?
  self.level == Logger::DEBUG
end

.levelObject



50
51
52
# File 'lib/thin/logging.rb', line 50

def level
  @logger ? @logger.level : nil # or 'silent'
end

.level=(value) ⇒ Object



54
55
56
57
58
# File 'lib/thin/logging.rb', line 54

def level=(value)
  # If logging has been silenced, then re-enable logging
  @logger = Logger.new(STDOUT) if @logger.nil?
  @logger.level = value
end

.log_debug(msg = nil) ⇒ Object

Log a message at DEBUG level



142
143
144
# File 'lib/thin/logging.rb', line 142

def log_debug(msg=nil)
  Logging.log_msg(msg || yield, Logger::DEBUG)
end

.log_error(msg, e = nil) ⇒ Object

Log a message at ERROR level (and maybe a backtrace)



156
157
158
159
160
161
162
# File 'lib/thin/logging.rb', line 156

def log_error(msg, e=nil)
  log_msg = msg
  if e
    log_msg += ": #{e}\n\t" + e.backtrace.join("\n\t") + "\n"
  end
  Logging.log_msg(log_msg, Logger::ERROR)
end

.log_info(msg) ⇒ Object

Log a message at INFO level



149
150
151
# File 'lib/thin/logging.rb', line 149

def log_info(msg)
  Logging.log_msg(msg || yield, Logger::INFO)
end

.log_msg(msg, level = Logger::INFO) ⇒ Object



99
100
101
102
# File 'lib/thin/logging.rb', line 99

def log_msg(msg, level=Logger::INFO)
  return unless @logger
  @logger.add(level, msg)
end

.silent=(shh) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/thin/logging.rb', line 38

def silent=(shh)
  if shh
    @logger = nil
  else
    @logger ||= Logger.new(STDOUT)
  end
end

.silent?Boolean



46
47
48
# File 'lib/thin/logging.rb', line 46

def silent?
  !@logger.nil?
end

.trace(msg = nil) ⇒ Object

Log a message if tracing is activated



135
136
137
# File 'lib/thin/logging.rb', line 135

def trace(msg=nil)
  Logging.trace_msg(msg) if msg
end

.trace=(enabled) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/thin/logging.rb', line 26

def trace=(enabled)
  if enabled
    @trace_logger ||= Logger.new(STDOUT)
  else
    @trace_logger = nil
  end
end

.trace?Boolean



34
35
36
# File 'lib/thin/logging.rb', line 34

def trace?
  !@trace_logger.nil?
end

.trace_msg(msg) ⇒ Object



104
105
106
107
# File 'lib/thin/logging.rb', line 104

def trace_msg(msg)
  return unless @trace_logger
  @trace_logger.info(msg)
end

Instance Method Details

#log(msg) ⇒ Object

For backwards compatibility



167
168
169
170
171
# File 'lib/thin/logging.rb', line 167

def log msg
  STDERR.puts('#log has been deprecated, please use the ' \
              'log_level function instead (e.g. - log_info).')
  log_info(msg)
end

#log_debug(msg = nil) ⇒ Object

Log a message at DEBUG level



142
143
144
# File 'lib/thin/logging.rb', line 142

def log_debug(msg=nil)
  Logging.log_msg(msg || yield, Logger::DEBUG)
end

#log_error(msg, e = nil) ⇒ Object

Log a message at ERROR level (and maybe a backtrace)



156
157
158
159
160
161
162
# File 'lib/thin/logging.rb', line 156

def log_error(msg, e=nil)
  log_msg = msg
  if e
    log_msg += ": #{e}\n\t" + e.backtrace.join("\n\t") + "\n"
  end
  Logging.log_msg(log_msg, Logger::ERROR)
end

#log_info(msg) ⇒ Object

Log a message at INFO level



149
150
151
# File 'lib/thin/logging.rb', line 149

def log_info(msg)
  Logging.log_msg(msg || yield, Logger::INFO)
end

#silentObject



126
127
128
# File 'lib/thin/logging.rb', line 126

def silent
  Logging.silent?
end

#silent=(value) ⇒ Object



130
131
132
# File 'lib/thin/logging.rb', line 130

def silent=(value)
  Logging.silent = value
end

#trace(msg = nil) ⇒ Object

Log a message if tracing is activated



135
136
137
# File 'lib/thin/logging.rb', line 135

def trace(msg=nil)
  Logging.trace_msg(msg) if msg
end