class ANSI::Logger

ANSI::Logger

Extended variation of Ruby's standard Logger library that supports color output.

log = ANSI::Logger.new

log.formatter do |severity, timestamp, progname, msg|
  ANSI::Logger::SIMPLE_FORMAT % [severity, msg]
end

Constants

DETAILED_FORMAT
SIMPLE_FORMAT

Some available logging formats.

Public Instance Methods

ansicolor=(on) click to toggle source
# File lib/ansi/logger.rb, line 50
def ansicolor=(on)
  @logdev.ansicolor = on
end
ansicolor?() click to toggle source
# File lib/ansi/logger.rb, line 45
def ansicolor?
  @logdev.ansicolor?
end
debug(progname=nil, &block) click to toggle source
Calls superclass method
# File lib/ansi/logger.rb, line 97
def debug(progname=nil, &block)
  return unless debug?
  @logdev.ansicolor? ? debug_with_color{ super } : super
end
error(progname=nil, &block) click to toggle source
Calls superclass method
# File lib/ansi/logger.rb, line 103
def error(progname=nil, &block)
  return unless error?
  @logdev.ansicolor? ? error_with_color{ super } : super
end
fatal(progname=nil, &block) click to toggle source
Calls superclass method
# File lib/ansi/logger.rb, line 109
def fatal(progname=nil, &block)
  return unless error?
  @logdev.ansicolor? ? fatal_with_color{ super } : super
end
formatter(&block) click to toggle source

Dictate the way in which this logger should format the messages it displays. This method requires a block. The block should return formatted strings given severity, timestamp, progname and msg.

Example

logger = ANSI::Logger.new

logger.formatter do |severity, timestamp, progname, msg|
  "#{progname}@#{timestamp} - #{severity}::#{msg}"
end
Calls superclass method
# File lib/ansi/logger.rb, line 67
def formatter(&block)
  self.formatter = block if block
  super
end
info(progname=nil, &block) click to toggle source
Calls superclass method
# File lib/ansi/logger.rb, line 85
def info(progname=nil, &block)
  return unless info?
  @logdev.ansicolor? ? info_with_color{ super } : super
end
styles(options=nil) click to toggle source
# File lib/ansi/logger.rb, line 72
def styles(options=nil)
  @styles ||= {
    :info  => [],
    :warn  => [:yellow],
    :debug => [:cyan],
    :error => [:red],
    :fatal => [:bold, :red]
  }
  @styles.merge!(options) if options
  @styles
end
warn(progname=nil, &block) click to toggle source
Calls superclass method
# File lib/ansi/logger.rb, line 91
def warn(progname=nil, &block)
  return unless warn?
  @logdev.ansicolor? ? warn_with_color{ super } : super
end

Private Instance Methods

debug_with_color() { || ... } click to toggle source
# File lib/ansi/logger.rb, line 134
def debug_with_color #:yield:
  styles[:debug].each{ |s| self << ANSI::Code.send(s) }
  yield
  self << ANSI::Code.clear
end
error_with_color() { || ... } click to toggle source
# File lib/ansi/logger.rb, line 128
def error_with_color #:yield:
  styles[:error].each{ |s| self << ANSI::Code.send(s) }
  yield
  self << ANSI::Code.clear
end
fatal_with_color() { || ... } click to toggle source
# File lib/ansi/logger.rb, line 140
def fatal_with_color #:yield:
  styles[:fatal].each{ |s| self << ANSI::Code.send(s) }
  yield
  self << ANSI::Code.clear
end
info_with_color() { || ... } click to toggle source
# File lib/ansi/logger.rb, line 116
def info_with_color #:yield:
  styles[:info].each{ |s| self << ANSI::Code.send(s) }
  yield
  self << ANSI::Code.clear
end
warn_with_color() { || ... } click to toggle source
# File lib/ansi/logger.rb, line 122
def warn_with_color #:yield:
  styles[:warn].each{ |s| self << ANSI::Code.send(s) }
  yield
  self << ANSI::Code.clear
end