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

debug=(val) click to toggle source
# File lib/thin/logging.rb, line 115
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 112
def debug?
  self.level == Logger::DEBUG
end
level() click to toggle source
# File lib/thin/logging.rb, line 50
def level
  @logger ? @logger.level : nil # or 'silent'
end
level=(value) click to toggle source
# 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) { || ... } click to toggle source

Log a message at DEBUG level

# 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) click to toggle source

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

# 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) { || ... } click to toggle source

Log a message at INFO level

# 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) click to toggle source
# File lib/thin/logging.rb, line 99
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 63
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=(shh) click to toggle source
# File lib/thin/logging.rb, line 38
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 46
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 135
def trace(msg=nil)
  Logging.trace_msg(msg) if msg
end
trace=(enabled) click to toggle source
# File lib/thin/logging.rb, line 26
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 34
def trace?
  !@trace_logger.nil?
end
trace_logger=(custom_tracer) click to toggle source
# File lib/thin/logging.rb, line 81
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 104
def trace_msg(msg)
  return unless @trace_logger
  @trace_logger.info(msg)
end

Public Instance Methods

log(msg) click to toggle source

For backwards compatibility

# 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) { || ... } click to toggle source

Log a message at DEBUG level

# 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) click to toggle source

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

# 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) { || ... } click to toggle source

Log a message at INFO level

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

Log a message if tracing is activated

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