module Thin::Logging

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.

Attributes

logger[R]
trace_logger[R]

Public Class Methods

log_debug(msg=nil) { || ... } click to toggle source

Log a message at DEBUG level

# File lib/thin/logging.rb, line 140
def log_debug(msg=nil)
  Logging.log_msg(msg || yield, Logger::DEBUG)
end
log_error(msg, e=nil) click to toggle source

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

# File lib/thin/logging.rb, line 154
def log_error(msg, e=nil)
  log_msg = msg + ": #{e}\n\t" + e.backtrace.join("\n\t") + "\n" if e
  Logging.log_msg(log_msg, Logger::ERROR)
end
log_info(msg) { || ... } click to toggle source

Log a message at INFO level

# File lib/thin/logging.rb, line 147
def log_info(msg)
  Logging.log_msg(msg || yield, Logger::INFO)
end
trace(msg=nil) click to toggle source

Log a message if tracing is activated

# File lib/thin/logging.rb, line 133
def trace(msg=nil)
  Logging.trace_msg(msg) if msg
end

Public Instance Methods

debug=(val) click to toggle source
# File lib/thin/logging.rb, line 113
def debug=(val)
  self.level = (val ? Logger::DEBUG : Logger::INFO)
end
debug?() click to toggle source

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.

# File lib/thin/logging.rb, line 110
def debug?
  self.level == Logger::DEBUG
end
level() click to toggle source
# File lib/thin/logging.rb, line 48
def level
  @logger ? @logger.level : nil # or 'silent'
end
level=(value) click to toggle source
# File lib/thin/logging.rb, line 52
def level=(value)
  # If logging has been silenced, then re-enable logging
  @logger = Logger.new(STDOUT) if @logger.nil?
  @logger.level = value
end
log(msg) click to toggle source

For backwards compatibility

# File lib/thin/logging.rb, line 162
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) { || ... } click to toggle source

Log a message at DEBUG level

# File lib/thin/logging.rb, line 140
def log_debug(msg=nil)
  Logging.log_msg(msg || yield, Logger::DEBUG)
end
log_error(msg, e=nil) click to toggle source

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

# File lib/thin/logging.rb, line 154
def log_error(msg, e=nil)
  log_msg = msg + ": #{e}\n\t" + e.backtrace.join("\n\t") + "\n" if e
  Logging.log_msg(log_msg, Logger::ERROR)
end
log_info(msg) { || ... } click to toggle source

Log a message at INFO level

# File lib/thin/logging.rb, line 147
def log_info(msg)
  Logging.log_msg(msg || yield, Logger::INFO)
end
log_msg(msg, level=Logger::INFO) click to toggle source
# File lib/thin/logging.rb, line 97
def log_msg(msg, level=Logger::INFO)
  return unless @logger
  @logger.add(level, msg)
end
logger=(custom_logger) click to toggle source

Allow user to specify a custom logger to use. This object must respond to: level, level= and debug, info, warn, error, fatal

# File lib/thin/logging.rb, line 61
def logger=(custom_logger)
  [ :level   ,
    :level=  ,
    :debug   ,
    :info    ,
    :warn    ,
    :error   ,
    :fatal   ,
    :unknown ,
  ].each do |method|
    if not custom_logger.respond_to?(method)
      raise ArgumentError, "logger must respond to #{method}"
    end
  end

  @logger = custom_logger
end
silent() click to toggle source
# File lib/thin/logging.rb, line 124
def silent
  Logging.silent?
end
silent=(shh) click to toggle source
# File lib/thin/logging.rb, line 36
def silent=(shh)
  if shh
    @logger = nil
  else
    @logger ||= Logger.new(STDOUT)
  end
end
silent?() click to toggle source
# File lib/thin/logging.rb, line 44
def silent?
  !@logger.nil?
end
trace(msg=nil) click to toggle source

Log a message if tracing is activated

# File lib/thin/logging.rb, line 133
def trace(msg=nil)
  Logging.trace_msg(msg) if msg
end
trace=(enabled) click to toggle source
# File lib/thin/logging.rb, line 24
def trace=(enabled)
  if enabled
    @trace_logger ||= Logger.new(STDOUT)
  else
    @trace_logger = nil
  end
end
trace?() click to toggle source
# File lib/thin/logging.rb, line 32
def trace?
  !@trace_logger.nil?
end
trace_logger=(custom_tracer) click to toggle source
# File lib/thin/logging.rb, line 79
def trace_logger=(custom_tracer)
  [ :level   ,
    :level=  ,
    :debug   ,
    :info    ,
    :warn    ,
    :error   ,
    :fatal   ,
    :unknown ,
  ].each do |method|
    if not custom_tracer.respond_to?(method)
      raise ArgumentError, "trace logger must respond to #{method}"
    end
  end

  @trace_logger = custom_tracer
end
trace_msg(msg) click to toggle source
# File lib/thin/logging.rb, line 102
def trace_msg(msg)
  return unless @trace_logger
  @trace_logger.info(msg)
end