Class: Sunshine::Output
- Inherits:
-
Object
- Object
- Sunshine::Output
- Defined in:
- lib/sunshine/output.rb
Overview
The Output class handles all of the logging to the shell during the Sunshine runtime.
Constant Summary collapse
- COLORS =
{ Logger::UNKNOWN => :red, Logger::FATAL => :red, Logger::ERROR => :red, Logger::WARN => :yellow, Logger::INFO => :default, Logger::DEBUG => :cyan, }
Instance Attribute Summary collapse
-
#indent ⇒ Object
Returns the value of attribute indent.
-
#level ⇒ Object
Returns the value of attribute level.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#debug(title, message, options = {}, &block) ⇒ Object
Log a message of log level debug.
-
#error(title, message, options = {}, &block) ⇒ Object
Log a message of log level error.
-
#fatal(title, message, options = {}, &block) ⇒ Object
Log a message of log level fatal.
-
#info(title, message, options = {}, &block) ⇒ Object
Log a message of log level info.
-
#initialize(options = {}) ⇒ Output
constructor
A new instance of Output.
-
#log(title, message, options = {}, &block) ⇒ Object
Generic log message which handles log indentation (for clarity).
-
#print(title, message, options = {}) ⇒ Object
Prints messages according to the standard output format.
-
#unknown(title, message, options = {}, &block) ⇒ Object
Log a message of log level unknown.
-
#warn(title, message, options = {}, &block) ⇒ Object
Log a message of log level warn.
Constructor Details
#initialize(options = {}) ⇒ Output
Returns a new instance of Output.
20 21 22 23 24 25 |
# File 'lib/sunshine/output.rb', line 20 def initialize(={}) @logger = Logger.new [:output] || $stdout @logger.formatter = lambda{|sev, time, progname, msg| msg} @level = @logger.level = [:level] || Logger::DEBUG @indent = 0 end |
Instance Attribute Details
#indent ⇒ Object
Returns the value of attribute indent.
18 19 20 |
# File 'lib/sunshine/output.rb', line 18 def indent @indent end |
#level ⇒ Object
Returns the value of attribute level.
18 19 20 |
# File 'lib/sunshine/output.rb', line 18 def level @level end |
#logger ⇒ Object
Returns the value of attribute logger.
18 19 20 |
# File 'lib/sunshine/output.rb', line 18 def logger @logger end |
Instance Method Details
#debug(title, message, options = {}, &block) ⇒ Object
Log a message of log level debug.
127 128 129 |
# File 'lib/sunshine/output.rb', line 127 def debug(title, , ={}, &block) self.log(title, , .merge(:level => Logger::DEBUG), &block) end |
#error(title, message, options = {}, &block) ⇒ Object
Log a message of log level error.
109 110 111 |
# File 'lib/sunshine/output.rb', line 109 def error(title, , ={}, &block) self.log(title, , .merge(:level => Logger::ERROR), &block) end |
#fatal(title, message, options = {}, &block) ⇒ Object
Log a message of log level fatal.
103 104 105 |
# File 'lib/sunshine/output.rb', line 103 def fatal(title, , ={}, &block) self.log(title, , .merge(:level => Logger::FATAL), &block) end |
#info(title, message, options = {}, &block) ⇒ Object
Log a message of log level info.
121 122 123 |
# File 'lib/sunshine/output.rb', line 121 def info(title, , ={}, &block) self.log(title, , .merge(:level => Logger::INFO), &block) end |
#log(title, message, options = {}, &block) ⇒ Object
Generic log message which handles log indentation (for clarity). Log indentation if achieved done by passing a block:
output.log("MAIN", "Main thing is happening") do
...
output.log("SUB1", "Sub process thing") do
...
output.log("SUB2", "Innermost process thing")
end
end
output.log("MAIN", "Start something else")
------
> [MAIN] Main thing is happening
> [SUB1] Sub process thing
> [SUB2] Innermost process thing
>
> [MAIN] Start something else
Log level is set to the instance’s default unless specified in the options argument with :level => Logger::LEVEL. The default log level is Logger::INFO
Best practice for using log levels is to call the level methods which all work similarly to the log method: unknown, fatal, error, warn, info, debug
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sunshine/output.rb', line 76 def log(title, , ={}, &block) unless Sunshine.trace? return block.call if block_given? return end = {:indent => @indent}.merge() self.print(title, , ) if block_given? @indent = @indent + 1 begin block.call ensure @indent = @indent - 1 unless @indent <= 0 @logger << "\n" end end end |
#print(title, message, options = {}) ⇒ Object
Prints messages according to the standard output format. Options supported:
:level: level of the log message
:indent: indentation of the message
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sunshine/output.rb', line 32 def print(title, , ={}) severity = [:level] || Logger::DEBUG color = COLORS[severity] [:indent] = 0 if [:indent] < 0 indent = " " * ([:indent].to_i * 2) print_string = .split("\n") print_string.map!{|m| "#{indent}[#{title}] #{m.chomp}"} print_string = "#{print_string.join("\n")} \n" print_string = print_string.foreground(color) print_string = print_string.bright if indent.empty? @logger.add(severity, print_string) end |
#unknown(title, message, options = {}, &block) ⇒ Object
Log a message of log level unknown.
97 98 99 |
# File 'lib/sunshine/output.rb', line 97 def unknown(title, , ={}, &block) self.log(title, , .merge(:level => Logger::UNKNOWN), &block) end |
#warn(title, message, options = {}, &block) ⇒ Object
Log a message of log level warn.
115 116 117 |
# File 'lib/sunshine/output.rb', line 115 def warn(title, , ={}, &block) self.log(title, , .merge(:level => Logger::WARN), &block) end |