Class | Logging::ColorScheme |
In: |
lib/logging/color_scheme.rb
|
Parent: | Object |
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.
CLEAR | = | "\e[0m".freeze | Embed in a String to clear all previous ANSI sequences. This MUST be done before the program exits! | |
RESET | = | CLEAR # An alias for CLEAR. | ||
ERASE_LINE | = | "\e[K".freeze # Erase the current line of terminal output. | ||
ERASE_CHAR | = | "\e[P".freeze # Erase the character under the cursor. | ||
BOLD | = | "\e[1m".freeze # The start of an ANSI bold sequence. | ||
DARK | = | "\e[2m".freeze # The start of an ANSI dark sequence. (Terminal support uncommon.) | ||
UNDERLINE | = | "\e[4m".freeze # The start of an ANSI underline sequence. | ||
UNDERSCORE | = | UNDERLINE # An alias for UNDERLINE. | ||
BLINK | = | "\e[5m".freeze # The start of an ANSI blink sequence. (Terminal support uncommon.) | ||
REVERSE | = | "\e[7m".freeze # The start of an ANSI reverse sequence. | ||
CONCEALED | = | "\e[8m".freeze # The start of an ANSI concealed sequence. (Terminal support uncommon.) | ||
BLACK | = | "\e[30m".freeze # Set the terminal's foreground ANSI color to black. | ||
RED | = | "\e[31m".freeze # Set the terminal's foreground ANSI color to red. | ||
GREEN | = | "\e[32m".freeze # Set the terminal's foreground ANSI color to green. | ||
YELLOW | = | "\e[33m".freeze # Set the terminal's foreground ANSI color to yellow. | ||
BLUE | = | "\e[34m".freeze # Set the terminal's foreground ANSI color to blue. | ||
MAGENTA | = | "\e[35m".freeze # Set the terminal's foreground ANSI color to magenta. | ||
CYAN | = | "\e[36m".freeze # Set the terminal's foreground ANSI color to cyan. | ||
WHITE | = | "\e[37m".freeze # Set the terminal's foreground ANSI color to white. | ||
ON_BLACK | = | "\e[40m".freeze # Set the terminal's background ANSI color to black. | ||
ON_RED | = | "\e[41m".freeze # Set the terminal's background ANSI color to red. | ||
ON_GREEN | = | "\e[42m".freeze # Set the terminal's background ANSI color to green. | ||
ON_YELLOW | = | "\e[43m".freeze # Set the terminal's background ANSI color to yellow. | ||
ON_BLUE | = | "\e[44m".freeze # Set the terminal's background ANSI color to blue. | ||
ON_MAGENTA | = | "\e[45m".freeze # Set the terminal's background ANSI color to magenta. | ||
ON_CYAN | = | "\e[46m".freeze # Set the terminal's background ANSI color to cyan. | ||
ON_WHITE | = | "\e[47m".freeze # Set the terminal's background ANSI color to white. |
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.
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.