Cinch::Logger::FormattedLogger

A formatted logger that will colorize individual parts of IRC messages.

Constants

COLORS

Public Class Methods

new(output = STDERR) click to toggle source

@param [IO] output An IO to log to.

# File lib/cinch/logger/formatted_logger.rb, line 17
def initialize(output = STDERR)
  @output = output
  @mutex = Mutex.new
end

Public Instance Methods

colorize(text, *codes) click to toggle source

@api private @param [String] text text to colorize @param [Array<Symbol>] codes array of colors to apply @return [String] colorized string

# File lib/cinch/logger/formatted_logger.rb, line 67
def colorize(text, *codes)
  return text unless @output.tty?
  COLORS.values_at(*codes).join + text + COLORS[:reset]
end
debug(messages) click to toggle source

(see Logger::Logger#debug)

# File lib/cinch/logger/formatted_logger.rb, line 23
def debug(messages)
  log(messages, :debug)
end
log(messages, kind = :generic) click to toggle source

(see Logger::Logger#log)

# File lib/cinch/logger/formatted_logger.rb, line 28
def log(messages, kind = :generic)
  @mutex.synchronize do
    messages = [messages].flatten.map {|s| s.to_s.chomp}
    messages.each do |msg|
      message = Time.now.strftime("[%Y/%m/%d %H:%M:%S.%L] ")
      if kind == :debug
        prefix = colorize("!! ", :yellow)
        message << prefix + msg
      else
        pre, msg = msg.split(" :", 2)
        pre_parts = pre.split(" ")

        if kind == :incoming
          prefix = colorize(">> ", :green)

          if pre_parts.size == 1
            pre_parts[0] = colorize(pre_parts[0], :bold)
          else
            pre_parts[0] = colorize(pre_parts[0], :blue)
            pre_parts[1] = colorize(pre_parts[1], :bold)
          end

        elsif kind == :outgoing
          prefix = colorize("<< ", :red)
          pre_parts[0] = colorize(pre_parts[0], :bold)
        end

        message << prefix + pre_parts.join(" ")
        message << colorize(" :#{msg}", :yellow) if msg
      end
      @output.puts message.encode("locale", {:invalid => :replace, :undef => :replace})
    end
  end
end
log_exception(e) click to toggle source

(see Logger::Logger#log_exception)

# File lib/cinch/logger/formatted_logger.rb, line 73
def log_exception(e)
  lines = ["#{e.backtrace.first}: #{e.message} (#{e.class})"]
  lines.concat e.backtrace[1..-1].map {|s| "\t" + s}
  debug(lines)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.