Parent

Logging::ColorScheme

ColorScheme objects encapsulate a named set of colors to be used in the colors() method call. For example, by applying a ColorScheme that has a :warning color then the following could be used:

  scheme.color("This is a warning", :warning)

ColorScheme objects are used by the Pattern layout code to colorize log messages. Each color scheme is given a unique name which is used by the Pattern layout to lookup the appropriate color scheme to use. Please refer to the Pattern layout documentation for more details - specifically the initializer documentation.

The color scheme can be applied to the Pattern layout in several ways. Each token in the log pattern can be colorized with the log level (debug, info, warn, etc) receiving unique colors based on the level itself. Another option is to colorize the entire log message based on the log level; in this mode tokens do not get their own colors. Please see the ColorScheme initializer for the list of colorization options.

Constants

CLEAR

Embed in a String to clear all previous ANSI sequences. This MUST be done before the program exits!

RESET
ERASE_LINE
ERASE_CHAR
BOLD
DARK
UNDERLINE
UNDERSCORE
BLINK
REVERSE
CONCEALED
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
ON_BLACK
ON_RED
ON_GREEN
ON_YELLOW
ON_BLUE
ON_MAGENTA
ON_CYAN
ON_WHITE

Public Class Methods

[]( name ) click to toggle source

Retrieve a color scheme by name.

    # File lib/logging/color_scheme.rb, line 35
35:       def []( name )
36:         @color_schemes[name.to_s]
37:       end
[]=( name, value ) click to toggle source

Store a color scheme by name.

    # File lib/logging/color_scheme.rb, line 41
41:       def []=( name, value )
42:         raise ArgumentError, "Silly! That's not a ColorSchmeme!" unless ColorScheme === value
43:         @color_schemes[name.to_s] = value
44:       end
new( name, opts = {} ) click to toggle source

Create a ColorScheme instance that can be accessed using the given name. If a color scheme already exists with the given name it will be replaced by the new color scheme.

The color names are passed as options to the method with each name mapping to one or more color codes. For example:

   ColorScheme.new('example', :logger => [:white, :on_green], :message => :magenta)

The color codes are the lowercase names of the constants defined at the end of this file. Multiple color codes can be aliased by grouping them in an array as shown in the example above.

Since color schemes are primary intended to be used with the Pattern layout, there are a few special options of note. First the log levels are enumerated in their own hash:

   :levels => {
     :debug => :blue,
     :info  => :cyan,
     :warn  => :yellow,
     :error => :red,
     :fatal => [:white, :on_red]
   }

The log level token will be colorized differently based on the value of the log level itself. Similarly the entire log message can be colorized based on the value of the log level. A different option should be given for this behavior:

   :lines => {
     :debug => :blue,
     :info  => :cyan,
     :warn  => :yellow,
     :error => :red,
     :fatal => [:white, :on_red]
   }

The :levels and :lines options cannot be used together; only one or the other should be given.

The remaining tokens defined in the Pattern layout can be colorized using the following aliases. Their meaning in the Pattern layout are repeated here for sake of clarity.

   :logger       [%c] name of the logger that generate the log event
   :date         [%d] datestamp
   :message      [%m] the user supplied log message
   :pid          [%p] PID of the current process
   :time         [%r] the time in milliseconds since the program started
   :thread       [%T] the name of the thread Thread.current[:name]
   :thread_id    [%t] object_id of the thread
   :file         [%F] filename where the logging request was issued
   :line         [%L] line number where the logging request was issued
   :method       [%M] method name where the logging request was issued

Please refer to the “examples/colorization.rb“ file for a working example of log colorization.

     # File lib/logging/color_scheme.rb, line 120
120:     def initialize( name, opts = {} )
121:       @scheme = Hash.new
122: 
123:       @lines = opts.key? :lines
124:       @levels = opts.key? :levels
125:       raise ArgumentError, "Found both :lines and :levels - only one can be used." if lines? and levels?
126: 
127:       lines = opts.delete :lines
128:       levels = opts.delete :levels
129: 
130:       load_from_hash(opts)
131:       load_from_hash(lines) if lines?
132:       load_from_hash(levels) if levels?
133: 
134:       ::Logging::ColorScheme[name] = self
135:     end
reset() click to toggle source

Clear all color schemes and setup a default color scheme.

    # File lib/logging/color_scheme.rb, line 48
48:       def reset
49:         @color_schemes ||= {}
50:         @color_schemes.clear
51: 
52:         new(:default, :levels => {
53:           :info  => :green,
54:           :warn  => :yellow,
55:           :error => :red,
56:           :fatal => [:white, :on_red]
57:         })
58:       end

Public Instance Methods

[]( color_tag ) click to toggle source

Allow the scheme to be accessed like a Hash.

     # File lib/logging/color_scheme.rb, line 165
165:     def []( color_tag )
166:       @scheme[to_key(color_tag)]
167:     end
[]=( color_tag, constants ) click to toggle source

Allow the scheme to be set like a Hash.

     # File lib/logging/color_scheme.rb, line 171
171:     def []=( color_tag, constants )
172:       @scheme[to_key(color_tag)] = constants.respond_to?(:map) ?
173:           constants.map { |c| to_constant(c) }.join : to_constant(constants)
174:     end
color( string, *colors ) click to toggle source

This method provides easy access to ANSI color sequences, without the user needing to remember to CLEAR at the end of each sequence. Just pass the string to color, followed by a list of colors you would like it to be affected by. The colors can be ColorScheme class constants, or symbols (:blue for BLUE, for example). A CLEAR will automatically be embedded to the end of the returned String.

     # File lib/logging/color_scheme.rb, line 183
183:     def color( string, *colors )
184:       colors.map! { |color|
185:         color_tag = to_key(color)
186:         @scheme.key?(color_tag) ? @scheme[color_tag] : to_constant(color)
187:       }
188: 
189:       colors.compact!
190:       return string if colors.empty?
191: 
192:       "#{colors.join}#{string}#{CLEAR}"
193:     end
include?( color_tag ) click to toggle source

Does this color scheme include the given tag name?

     # File lib/logging/color_scheme.rb, line 159
159:     def include?( color_tag )
160:       @scheme.key?(to_key(color_tag))
161:     end
levels?() click to toggle source

Returns true if the :levels option was passed to the constructor.

     # File lib/logging/color_scheme.rb, line 153
153:     def levels?
154:       @levels
155:     end
lines?() click to toggle source

Returns true if the :lines option was passed to the constructor.

     # File lib/logging/color_scheme.rb, line 147
147:     def lines?
148:       @lines
149:     end
load_from_hash( h ) click to toggle source

Load multiple colors from key/value pairs.

     # File lib/logging/color_scheme.rb, line 139
139:     def load_from_hash( h )
140:       h.each_pair do |color_tag, constants|
141:         self[color_tag] = constants
142:       end
143:     end

Private Instance Methods

to_constant( v ) click to toggle source

Return a normalized representation of a color setting.

     # File lib/logging/color_scheme.rb, line 205
205:     def to_constant( v )
206:       ColorScheme.const_get(v.to_s.upcase)
207:     rescue NameError
208:       return  nil
209:     end
to_key( t ) click to toggle source

Return a normalized representation of a color name.

     # File lib/logging/color_scheme.rb, line 199
199:     def to_key( t )
200:       t.to_s.downcase
201:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.