Class: Cinch::Logger::FormattedLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/cinch/logger/formatted_logger.rb

Overview

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

Constant Summary collapse

COLORS =
{
  :reset => "\e[0m",
  :bold => "\e[1m",
  :red => "\e[31m",
  :green => "\e[32m",
  :yellow => "\e[33m",
  :blue => "\e[34m",
}

Instance Method Summary collapse

Constructor Details

#initialize(output = STDERR) ⇒ FormattedLogger

Returns a new instance of FormattedLogger.

Parameters:

  • output (IO) (defaults to: STDERR)

    An IO to log to.



17
18
19
20
# File 'lib/cinch/logger/formatted_logger.rb', line 17

def initialize(output = STDERR)
  @output = output
  @mutex = Mutex.new
end

Instance Method Details

#colorize(text, *codes) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns colorized string.

Parameters:

  • text (String)

    text to colorize

  • codes (Array<Symbol>)

    array of colors to apply

Returns:

  • (String)

    colorized string



67
68
69
70
# 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) ⇒ void

This method returns an undefined value.

This method can be used by plugins to log custom messages.

Parameters:

  • message (String)

    The message to log



23
24
25
# File 'lib/cinch/logger/formatted_logger.rb', line 23

def debug(messages)
  log(messages, :debug)
end

#log(messages, kind = :generic) ⇒ void

This method returns an undefined value.

This method is used by #debug and #log_exception to log messages, and also by the IRC parser to log incoming and outgoing messages. You should not have to call this.

Parameters:

  • message (String)

    The message to log

  • kind (Symbol<:debug, :generic, :incoming, :outgoing>) (defaults to: :generic)

    The kind of message to log



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ void

This method returns an undefined value.

This method is used for logging messages.

Parameters:

  • e (Exception)

    The exception to log



73
74
75
76
77
# 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