Class: Beaker::Logger
- Inherits:
-
Object
- Object
- Beaker::Logger
- Defined in:
- lib/beaker/logger.rb
Overview
The Beaker Logger class This class handles message reporting for Beaker, it reports based upon a provided log level to a given destination (be it a string or file)
Constant Summary collapse
- NORMAL =
"\e[00;00m"
- BRIGHT_NORMAL =
"\e[00;01m"
- BLACK =
"\e[00;30m"
- RED =
"\e[00;31m"
- GREEN =
"\e[00;32m"
- YELLOW =
"\e[00;33m"
- BLUE =
"\e[00;34m"
- MAGENTA =
"\e[00;35m"
- CYAN =
"\e[00;36m"
- WHITE =
"\e[00;37m"
- GREY =
"\e[01;30m"
- BRIGHT_RED =
"\e[01;31m"
- BRIGHT_GREEN =
"\e[01;32m"
- BRIGHT_YELLOW =
"\e[01;33m"
- BRIGHT_BLUE =
"\e[01;34m"
- BRIGHT_MAGENTA =
"\e[01;35m"
- BRIGHT_CYAN =
"\e[01;36m"
- BRIGHT_WHITE =
"\e[01;37m"
- LOG_LEVELS =
The defined log levels. Each log level also reports messages at levels lower than itself
{ :debug => 5, :verbose => 3, :info => 2, :notify => 1, :warn => 0, }
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#destinations ⇒ Object
Returns the value of attribute destinations.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
Instance Method Summary collapse
-
#add_destination(dest) ⇒ Object
Construct an array of open steams for printing log messages to.
-
#debug(*args) ⇒ Object
Report a debug message.
-
#error(*args) ⇒ Object
Report an error message.
-
#host_output(*args) ⇒ Object
Custom reporting for messages generated by host SUTs.
-
#info(*args) ⇒ Object
Report an info message.
-
#initialize(*args) ⇒ Logger
constructor
Initialization of the Logger class.
-
#is_debug? ⇒ Boolean
Are we at LOG_LEVELS debug?.
-
#is_info? ⇒ Boolean
Are we at LOG_LEVELS info?.
-
#is_notify? ⇒ Boolean
Are we at LOG_LEVELS notify?.
-
#is_verbose? ⇒ Boolean
Are we at LOG_LEVELS verbose?.
-
#is_warn? ⇒ Boolean
Are we at LOG_LEVELS warn?.
-
#notify(*args) ⇒ Object
Report a notify message.
-
#optionally_color(color_code, msg, add_newline = true) ⇒ Object
Print the provided message to the set destination streams, using color codes if appropriate.
-
#pretty_backtrace(backtrace = caller(1)) ⇒ String
Utility method to get the current call stack and format it to a human-readable string (which some IDEs/editors will recognize as links to the line numbers in the trace).
-
#remove_destination(dest) ⇒ Object
Remove a steam from the destinations array based upon it’s name or file path.
-
#strip_colors_from(lines) ⇒ Array<String>
Strip any color codes from provided string(s).
-
#success(*args) ⇒ Object
Report a success message.
-
#warn(*args) ⇒ Object
Report a warning message.
Constructor Details
#initialize(dests) ⇒ Logger #initialize(dests, options) ⇒ Logger
Initialization of the Logger class
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/beaker/logger.rb', line 50 def initialize(*args) = args.last.is_a?(Hash) ? args.pop : {} @color = [:color] case [:log_level] when /debug/i @log_level = :debug when /verbose/i @log_level = :verbose when /info/i @log_level = :info when /notify/i @log_level = :notify when /warn/i @log_level = :warn else @log_level = :info end @destinations = [] dests = args dests << STDOUT unless [:quiet] dests.uniq! dests.each {|dest| add_destination(dest)} end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
35 36 37 |
# File 'lib/beaker/logger.rb', line 35 def color @color end |
#destinations ⇒ Object
Returns the value of attribute destinations.
35 36 37 |
# File 'lib/beaker/logger.rb', line 35 def destinations @destinations end |
#log_level ⇒ Object
Returns the value of attribute log_level.
35 36 37 |
# File 'lib/beaker/logger.rb', line 35 def log_level @log_level end |
Instance Method Details
#add_destination(dest) ⇒ Object
Construct an array of open steams for printing log messages to
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/beaker/logger.rb', line 77 def add_destination(dest) case dest when IO @destinations << dest when String @destinations << File.open(dest, 'w') else raise "Unsuitable log destination #{dest.inspect}" end end |
#debug(*args) ⇒ Object
Report a debug message.
Will not print unless we are at LOG_LEVELS ‘debug’ or higher.
145 146 147 148 |
# File 'lib/beaker/logger.rb', line 145 def debug *args return unless is_verbose? optionally_color WHITE, args end |
#error(*args) ⇒ Object
Report an error message.
Will always be reported.
186 187 188 |
# File 'lib/beaker/logger.rb', line 186 def error *args optionally_color BRIGHT_RED, args end |
#host_output(*args) ⇒ Object
Custom reporting for messages generated by host SUTs.
Will not print unless we are at LOG_LEVELS ‘debug’ or higher.
Strips any color codes already in the provided messages, then adds logger color codes before reporting
135 136 137 138 139 140 |
# File 'lib/beaker/logger.rb', line 135 def host_output *args return unless is_debug? strings = strip_colors_from args string = strings.join optionally_color GREY, string, false end |
#info(*args) ⇒ Object
Report an info message.
Will not print unless we are at LOG_LEVELS ‘info’ or higher.
163 164 165 166 |
# File 'lib/beaker/logger.rb', line 163 def info *args return unless is_info? optionally_color BLUE, args end |
#is_debug? ⇒ Boolean
Are we at LOG_LEVELS debug?
103 104 105 |
# File 'lib/beaker/logger.rb', line 103 def is_debug? LOG_LEVELS[@log_level] >= LOG_LEVELS[:debug] end |
#is_info? ⇒ Boolean
Are we at LOG_LEVELS info?
121 122 123 |
# File 'lib/beaker/logger.rb', line 121 def is_info? LOG_LEVELS[@log_level] >= LOG_LEVELS[:info] end |
#is_notify? ⇒ Boolean
Are we at LOG_LEVELS notify?
127 128 129 |
# File 'lib/beaker/logger.rb', line 127 def is_notify? LOG_LEVELS[@log_level] >= LOG_LEVELS[:notify] end |
#is_verbose? ⇒ Boolean
Are we at LOG_LEVELS verbose?
109 110 111 |
# File 'lib/beaker/logger.rb', line 109 def is_verbose? LOG_LEVELS[@log_level] >= LOG_LEVELS[:verbose] end |
#is_warn? ⇒ Boolean
Are we at LOG_LEVELS warn?
115 116 117 |
# File 'lib/beaker/logger.rb', line 115 def is_warn? LOG_LEVELS[@log_level] >= LOG_LEVELS[:warn] end |
#notify(*args) ⇒ Object
Report a notify message.
Will not print unless we are at LOG_LEVELS ‘notify’ or higher.
178 179 180 181 |
# File 'lib/beaker/logger.rb', line 178 def notify *args return unless is_notify? optionally_color BRIGHT_WHITE, args end |
#optionally_color(color_code, msg, add_newline = true) ⇒ Object
Print the provided message to the set destination streams, using color codes if appropriate
203 204 205 206 207 208 209 210 |
# File 'lib/beaker/logger.rb', line 203 def optionally_color color_code, msg, add_newline = true print_statement = add_newline ? :puts : :print @destinations.each do |to| to.print color_code if @color to.send print_statement, msg to.print NORMAL if @color end end |
#pretty_backtrace(backtrace = caller(1)) ⇒ String
Utility method to get the current call stack and format it to a human-readable string (which some IDEs/editors will recognize as links to the line numbers in the trace). Beaker associated files will be purged from backtrace unless log level is ‘debug’ or higher
218 219 220 221 |
# File 'lib/beaker/logger.rb', line 218 def pretty_backtrace backtrace = caller(1) trace = is_debug? ? backtrace : purge_harness_files_from( backtrace ) ( trace ).join "\n" end |
#remove_destination(dest) ⇒ Object
Remove a steam from the destinations array based upon it’s name or file path
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/beaker/logger.rb', line 90 def remove_destination(dest) case dest when IO @destinations.delete(dest) when String @destinations.delete_if {|d| d.respond_to?(:path) and d.path == dest} else raise "Unsuitable log destination #{dest.inspect}" end end |
#strip_colors_from(lines) ⇒ Array<String>
Strip any color codes from provided string(s)
193 194 195 196 197 |
# File 'lib/beaker/logger.rb', line 193 def strip_colors_from lines Array(lines).map do |line| line.gsub /\e\[(\d+;)?\d+m/, '' end end |
#success(*args) ⇒ Object
Report a success message.
Will always be reported.
171 172 173 |
# File 'lib/beaker/logger.rb', line 171 def success *args optionally_color GREEN, args end |
#warn(*args) ⇒ Object
Report a warning message. Will not print unless we are at LOG_LEVELS ‘warn’ or higher.
Will pre-pend the message with “Warning: ”.
154 155 156 157 158 |
# File 'lib/beaker/logger.rb', line 154 def warn *args return unless is_warn? strings = args.map {|msg| "Warning: #{msg}" } optionally_color YELLOW, strings end |