Class: Logsly::Logging182::ColorScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/logsly/logging182/color_scheme.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ ColorScheme

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.

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/logsly/logging182/color_scheme.rb', line 120

def initialize( name, opts = {} )
  @scheme = Hash.new

  @lines = opts.key? :lines
  @levels = opts.key? :levels
  raise ArgumentError, "Found both :lines and :levels - only one can be used." if lines? and levels?

  lines = opts.delete :lines
  levels = opts.delete :levels

  load_from_hash(opts)
  load_from_hash(lines) if lines?
  load_from_hash(levels) if levels?

  ::Logsly::Logging182::ColorScheme[name] = self
end

Class Method Details

.[](name) ⇒ Object

Retrieve a color scheme by name.



35
36
37
# File 'lib/logsly/logging182/color_scheme.rb', line 35

def []( name )
  @color_schemes[name.to_s]
end

.[]=(name, value) ⇒ Object

Store a color scheme by name.

Raises:

  • (ArgumentError)


41
42
43
44
# File 'lib/logsly/logging182/color_scheme.rb', line 41

def []=( name, value )
  raise ArgumentError, "Silly! That's not a ColorSchmeme!" unless ColorScheme === value
  @color_schemes[name.to_s] = value
end

.resetObject

Clear all color schemes and setup a default color scheme.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/logsly/logging182/color_scheme.rb', line 48

def reset
  @color_schemes ||= {}
  @color_schemes.clear

  new(:default, :levels => {
    :info  => :green,
    :warn  => :yellow,
    :error => :red,
    :fatal => [:white, :on_red]
  })
end

Instance Method Details

#[](color_tag) ⇒ Object

Allow the scheme to be accessed like a Hash.



165
166
167
# File 'lib/logsly/logging182/color_scheme.rb', line 165

def []( color_tag )
  @scheme[to_key(color_tag)]
end

#[]=(color_tag, constants) ⇒ Object

Allow the scheme to be set like a Hash.



171
172
173
174
# File 'lib/logsly/logging182/color_scheme.rb', line 171

def []=( color_tag, constants )
  @scheme[to_key(color_tag)] = constants.respond_to?(:map) ?
      constants.map { |c| to_constant(c) }.join : to_constant(constants)
end

#color(string, *colors) ⇒ Object

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.



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/logsly/logging182/color_scheme.rb', line 183

def color( string, *colors )
  colors.map! { |color|
    color_tag = to_key(color)
    @scheme.key?(color_tag) ? @scheme[color_tag] : to_constant(color)
  }

  colors.compact!
  return string if colors.empty?

  "#{colors.join}#{string}#{CLEAR}"
end

#include?(color_tag) ⇒ Boolean

Does this color scheme include the given tag name?

Returns:

  • (Boolean)


159
160
161
# File 'lib/logsly/logging182/color_scheme.rb', line 159

def include?( color_tag )
  @scheme.key?(to_key(color_tag))
end

#levels?Boolean

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

Returns:

  • (Boolean)


153
154
155
# File 'lib/logsly/logging182/color_scheme.rb', line 153

def levels?
  @levels
end

#lines?Boolean

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

Returns:

  • (Boolean)


147
148
149
# File 'lib/logsly/logging182/color_scheme.rb', line 147

def lines?
  @lines
end

#load_from_hash(h) ⇒ Object

Load multiple colors from key/value pairs.



139
140
141
142
143
# File 'lib/logsly/logging182/color_scheme.rb', line 139

def load_from_hash( h )
  h.each_pair do |color_tag, constants|
    self[color_tag] = constants
  end
end