Class: ANSI::Logger

Inherits:
Logger show all
Defined in:
lib/ansi/logger.rb

Overview

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

– TODO: What’s all this about then?

When using debug level logger messages always append ‘if $DBG’ at the end. This hack is needed because Ruby does not support lazy evaluation (lisp macros). ++

Constant Summary collapse

SIMPLE_FORMAT =

Some available logging formats.

"%5s: %s\n"
DETAILED_FORMAT =
"%s %5s: %s\n"

Instance Method Summary collapse

Instance Method Details

#ansicolor=(on) ⇒ Object



50
51
52
# File 'lib/ansi/logger.rb', line 50

def ansicolor=(on)
  @logdev.ansicolor = on
end

#ansicolor?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ansi/logger.rb', line 45

def ansicolor?
  @logdev.ansicolor?
end

#debug(progname = nil, &block) ⇒ Object



97
98
99
100
# File 'lib/ansi/logger.rb', line 97

def debug(progname=nil, &block)
  return unless debug?
  @logdev.ansicolor? ? debug_with_color{ super } : super
end

#debug_with_colorObject (private)

:yield:



134
135
136
137
138
# 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(progname = nil, &block) ⇒ Object



103
104
105
106
# File 'lib/ansi/logger.rb', line 103

def error(progname=nil, &block)
  return unless error?
  @logdev.ansicolor? ? error_with_color{ super } : super
end

#error_with_colorObject (private)

:yield:



128
129
130
131
132
# 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(progname = nil, &block) ⇒ Object



109
110
111
112
# File 'lib/ansi/logger.rb', line 109

def fatal(progname=nil, &block)
  return unless error?
  @logdev.ansicolor? ? fatal_with_color{ super } : super
end

#fatal_with_colorObject (private)

:yield:



140
141
142
143
144
# 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

#formatter(&block) ⇒ Object

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


67
68
69
70
# File 'lib/ansi/logger.rb', line 67

def formatter(&block)
  self.formatter = block if block
  super
end

#info(progname = nil, &block) ⇒ Object



85
86
87
88
# File 'lib/ansi/logger.rb', line 85

def info(progname=nil, &block)
  return unless info?
  @logdev.ansicolor? ? info_with_color{ super } : super
end

#info_with_colorObject (private)

:yield:



116
117
118
119
120
# 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

#styles(options = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



91
92
93
94
# File 'lib/ansi/logger.rb', line 91

def warn(progname=nil, &block)
  return unless warn?
  @logdev.ansicolor? ? warn_with_color{ super } : super
end

#warn_with_colorObject (private)

:yield:



122
123
124
125
126
# 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