Class: Cinch::Logger::FormattedLogger
- 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
-
#colorize(text, *codes) ⇒ String
private
Colorized string.
-
#debug(messages) ⇒ void
This method can be used by plugins to log custom messages.
-
#initialize(output = STDERR) ⇒ FormattedLogger
constructor
A new instance of FormattedLogger.
-
#log(messages, kind = :generic) ⇒ void
This method is used by #debug and #log_exception to log messages, and also by the IRC parser to log incoming and outgoing messages.
-
#log_exception(e) ⇒ void
This method is used for logging messages.
Constructor Details
#initialize(output = STDERR) ⇒ FormattedLogger
Returns a new instance of FormattedLogger.
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.
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.
23 24 25 |
# File 'lib/cinch/logger/formatted_logger.rb', line 23 def debug() log(, :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.
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(, kind = :generic) @mutex.synchronize do = [].flatten.map {|s| s.to_s.chomp} .each do |msg| = Time.now.strftime("[%Y/%m/%d %H:%M:%S.%L] ") if kind == :debug prefix = colorize("!! ", :yellow) << 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 << prefix + pre_parts.join(" ") << colorize(" :#{msg}", :yellow) if msg end @output.puts .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.
73 74 75 76 77 |
# File 'lib/cinch/logger/formatted_logger.rb', line 73 def log_exception(e) lines = ["#{e.backtrace.first}: #{e.} (#{e.class})"] lines.concat e.backtrace[1..-1].map {|s| "\t" + s} debug(lines) end |