Class: Blower::Logger
- 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
-
.indent ⇒ Object
The current indentation level.
-
.level ⇒ Object
The minimum severity level for which messages will be displayed.
Class Method Summary collapse
-
.define_helper(level) ⇒ Object
Define a helper method for a given severity level.
Instance Method Summary collapse
-
#debug(message, &block) ⇒ Object
Display a debug log message, as if by calling log directly.
-
#error(message, &block) ⇒ Object
Display a error log message, as if by calling log directly.
-
#fatal(message, &block) ⇒ Object
Display a fatal log message, as if by calling log directly.
-
#info(message, &block) ⇒ Object
Display a info log message, as if by calling log directly.
-
#initialize(prefix = "") ⇒ Logger
constructor
A new instance of Logger.
-
#log(level, message, quiet: false, &block) ⇒ Object
private
Display a log message.
- #thread ⇒ Object
-
#trace(message, &block) ⇒ Object
Display a trace log message, as if by calling log directly.
-
#warn(message, &block) ⇒ Object
Display a warn log message, as if by calling log directly.
-
#with_indent ⇒ Object
Yield with a temporarily incremented indent counter.
-
#with_prefix(string) ⇒ Object
Return a logger with the specified prefix.
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
.indent ⇒ Object
The current indentation level.
34 35 36 |
# File 'lib/blower/logger.rb', line 34 def indent @indent end |
.level ⇒ Object
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
Instance Method Details
#debug(message, &block) ⇒ Object
Display a debug log message, as if by calling log directly.
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.
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.
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.
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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/blower/logger.rb', line 64 def log (level, , quiet: false, &block) if !quiet && (LEVELS.index(level) >= LEVELS.index(Logger.level)) synchronize do = .to_s.colorize(COLORS[level]) if level = .to_s.colorize(COLORS[level]) if level .split("\n").each do |line| STDERR.puts " " * thread[:indent] + @prefix + line end end with_indent(&block) if block elsif block block.() end end |
#thread ⇒ Object
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.
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.
95 |
# File 'lib/blower/logger.rb', line 95 define_helper :warn |
#with_indent ⇒ Object
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 |