class Blower::Logger

Colorized logger.

Prints messages to STDOUT, colorizing them according to the specified log level.

The logging methods accept an optional block. Inside the block, log messages will be indented by two spaces. This works recursively.

Constants

COLORS

Colorize specifications for log levels.

LEVELS

Logging levels in ascending order of severity.

Attributes

indent[RW]

The current indentation level.

level[RW]

The minimum severity level for which messages will be displayed.

Public Class Methods

define_helper(level) click to toggle source

Define a helper method for a given severity level. @!macro [attach] log_helper

@!method $1(message, &block)
Display a $1 log message, as if by calling log directly.
@param [#to_s] message the message to display
@param block a block to execute with an indent after the message is displayed
@return the value of block, or nil
# File lib/blower/logger.rb, line 86
def self.define_helper (level)
  define_method(level) do |*args, **kwargs, &block|
    log(level, *args, **kwargs, &block)
  end
end
new(prefix = "") click to toggle source
Calls superclass method
# File lib/blower/logger.rb, line 39
def initialize (prefix = "")
  @prefix = prefix
  thread[:indent] = 0
  super()
end

Public Instance Methods

log(level, message, quiet: false, &block) click to toggle source

Display a log message. The block, if specified, is executed in an indented region after the log message is shown. @api private @param [Symbol] level the severity level @param [#to_s] message the message to display @param block a block to execute with an indent after the message is displayed @return the value of block, or nil

# File lib/blower/logger.rb, line 64
def log (level, message, quiet: false, &block)
  if !quiet && (LEVELS.index(level) >= LEVELS.index(Logger.level))
    synchronize do
      message = message.to_s.colorize(COLORS[level]) if level
      message = message.to_s.colorize(COLORS[level]) if level
      message.split("\n").each do |line|
        STDERR.puts "  " * thread[:indent] + @prefix + line
      end
    end
    with_indent(&block) if block
  elsif block
    block.()
  end
end
thread() click to toggle source
# File lib/blower/logger.rb, line 99
def thread
  Thread.current
end
with_indent() { || ... } click to toggle source

Yield with a temporarily incremented indent counter

# File lib/blower/logger.rb, line 51
def with_indent ()
  thread[:indent] += 1
  yield
ensure
  thread[:indent] -= 1
end
with_prefix(string) click to toggle source

Return a logger with the specified prefix

# File lib/blower/logger.rb, line 46
def with_prefix (string)
  Logger.send(:new, string)
end