Class: Blower::Logger

Inherits:
Object show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/blower/logger.rb

Overview

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.

Constant Summary collapse

LEVELS =

Logging levels in ascending order of severity.

%i(all trace debug info warn error fatal none)
COLORS =

Colorize specifications for log levels.

{
  trace: { color: :light_black },
  debug: { color: :default },
  info:  { color: :blue },
  warn:  { color: :yellow },
  error: { color: :red },
  fatal: { color: :light_white, background: :red },
}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = "") ⇒ Logger

Returns a new instance of Logger.


39
40
41
42
43
# File 'lib/blower/logger.rb', line 39

def initialize (prefix = "")
  @prefix = prefix
  thread[:indent] = 0
  super()
end

Class Attribute Details

.indentObject

The current indentation level.


34
35
36
# File 'lib/blower/logger.rb', line 34

def indent
  @indent
end

.levelObject

The minimum severity level for which messages will be displayed.


31
32
33
# File 'lib/blower/logger.rb', line 31

def level
  @level
end

Class Method Details

.define_helper(level) ⇒ Object

Define a helper method for a given severity level.


86
87
88
89
90
# 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

Instance Method Details

#debug(message, &block) ⇒ Object

Display a debug log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


93
# File 'lib/blower/logger.rb', line 93

define_helper :debug

#error(message, &block) ⇒ Object

Display a error log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


96
# File 'lib/blower/logger.rb', line 96

define_helper :error

#fatal(message, &block) ⇒ Object

Display a fatal log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


97
# File 'lib/blower/logger.rb', line 97

define_helper :fatal

#info(message, &block) ⇒ Object

Display a info log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


94
# File 'lib/blower/logger.rb', line 94

define_helper :info

#log(level, message, quiet: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Display a log message. The block, if specified, is executed in an indented region after the log message is shown.

Parameters:

  • level (Symbol)

    the severity level

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#threadObject


99
100
101
# File 'lib/blower/logger.rb', line 99

def thread
  Thread.current
end

#trace(message, &block) ⇒ Object

Display a trace log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


92
# File 'lib/blower/logger.rb', line 92

define_helper :trace

#warn(message, &block) ⇒ Object

Display a warn log message, as if by calling log directly.

Parameters:

  • message (#to_s)

    the message to display

  • block

    a block to execute with an indent after the message is displayed

Returns:

  • the value of block, or nil


95
# File 'lib/blower/logger.rb', line 95

define_helper :warn

#with_indentObject

Yield with a temporarily incremented indent counter


51
52
53
54
55
56
# File 'lib/blower/logger.rb', line 51

def with_indent ()
  thread[:indent] += 1
  yield
ensure
  thread[:indent] -= 1
end

#with_prefix(string) ⇒ Object

Return a logger with the specified prefix


46
47
48
# File 'lib/blower/logger.rb', line 46

def with_prefix (string)
  Logger.send(:new, string)
end