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 =
Some terms can’t handle grey, use normal
"\e[00;00m"
- 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"
- NONE =
""
- LOG_LEVELS =
The defined log levels. Each log level also reports messages at levels lower than itself
{ :trace => 6, :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.
-
#last_result ⇒ Object
The results of the most recently run command.
-
#line_prefix ⇒ Object
Determines the spacing that happens before an output line.
-
#log_colors ⇒ Object
Returns the value of attribute log_colors.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
Class Method Summary collapse
-
.generate_dated_log_folder(base_dir, log_prefix, timestamp) ⇒ String
Utility method to centralize dated log folder generation.
-
.strip_color_codes(text) ⇒ String
Remove color codes from provided string.
Instance Method Summary collapse
-
#add_destination(dest) ⇒ Object
Construct an array of open steams for printing log messages to.
-
#color_host_output(*args) ⇒ Object
Custom reporting for messages generated by host SUTs - to preserve output Will not print unless we are at LOG_LEVELS ‘verbose’ or higher.
-
#convert(string) ⇒ Object
Remove invalid UTF-8 codes from provided string(s).
-
#debug(*args) ⇒ Object
Report a debug message.
-
#error(*args) ⇒ Object
Report an error message.
-
#get_sublog ⇒ Object
Return the contents of the sublog.
-
#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_trace? ⇒ Boolean
Are we at LOG_LEVELS trace?.
-
#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.
-
#perf_output(*args) ⇒ Object
Custom reporting for performance/sysstat messages Will not print unless we are at LOG_LEVELS ‘debug’ or higher.
-
#prefix_log_line(line) ⇒ String
Prefixes a log line with the appropriate amount of whitespace for the level of test that’s running.
-
#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).
-
#quiet(off = true) ⇒ Object
Turn on/off STDOUT logging.
-
#remove_destination(dest) ⇒ Object
Remove a steam from the destinations array based upon it’s name or file path.
-
#start_sublog ⇒ Object
Create a new StringIO log to track the current output.
-
#step_in ⇒ Object
Sets the step level appropriately for logging to be indented correctly.
-
#step_out ⇒ Object
Sets the step level appropriately for logging to be indented correctly.
-
#strip_colors_from(lines) ⇒ Array<String>
Strip any color codes from provided string(s).
-
#success(*args) ⇒ Object
Report a success message.
-
#trace(*args) ⇒ Object
Report a trace message.
-
#warn(*args) ⇒ Object
Report a warning message.
Constructor Details
#initialize(dests) ⇒ Logger #initialize(dests, options) ⇒ Logger
Initialization of the Logger class
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/beaker/logger.rb', line 59 def initialize(*args) = args.last.is_a?(Hash) ? args.pop : {} @color = [:color] @sublog = nil case [:log_level] when /trace/i, :trace @log_level = :trace when /debug/i, :debug @log_level = :debug when /verbose/i, :verbose @log_level = :verbose when /info/i, :info @log_level = :info when /notify/i, :notify @log_level = :notify when /warn/i, :warn @log_level = :warn else @log_level = :verbose end @last_result = nil @line_prefix_length = 0 @line_prefix = '' @destinations = [] @log_colors = { :error => RED, :warn => BRIGHT_RED, :success => MAGENTA, :notify => BLUE, :info => GREEN, :debug => WHITE, :trace => BRIGHT_YELLOW, :perf => BRIGHT_MAGENTA, :host => YELLOW } @log_colors.merge!([:log_colors]) if [:log_colors] # if a user overrides any of the log_colors, we will no longer # override the colors at all on a CI build. This is b/c it is # assumed that if a user is overriding the colors, they know # what they are doing. We could potentially add an additional # option a user can pass to be explicit about still allowing # the override. unless [:log_colors] # Jenkins exposed variable - should be present on the slave directing # the beaker run ci_build = ENV['BUILD_NUMBER'] != nil @log_colors[:notify] = NORMAL if ci_build @log_colors[:info] = NORMAL if ci_build end 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.
44 45 46 |
# File 'lib/beaker/logger.rb', line 44 def color @color end |
#destinations ⇒ Object
Returns the value of attribute destinations.
44 45 46 |
# File 'lib/beaker/logger.rb', line 44 def destinations @destinations end |
#last_result ⇒ Object
The results of the most recently run command
9 10 11 |
# File 'lib/beaker/logger.rb', line 9 def last_result @last_result end |
#line_prefix ⇒ Object
Determines the spacing that happens before an output line
12 13 14 |
# File 'lib/beaker/logger.rb', line 12 def line_prefix @line_prefix end |
#log_colors ⇒ Object
Returns the value of attribute log_colors.
44 45 46 |
# File 'lib/beaker/logger.rb', line 44 def log_colors @log_colors end |
#log_level ⇒ Object
Returns the value of attribute log_level.
44 45 46 |
# File 'lib/beaker/logger.rb', line 44 def log_level @log_level end |
Class Method Details
.generate_dated_log_folder(base_dir, log_prefix, timestamp) ⇒ String
since this uses ‘mkdir -p’, log_prefix can be a number of nested directories
Utility method to centralize dated log folder generation
401 402 403 404 405 |
# File 'lib/beaker/logger.rb', line 401 def Logger.generate_dated_log_folder(base_dir, log_prefix, ) log_dir = File.join(base_dir, log_prefix, .strftime("%F_%H_%M_%S")) FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir) log_dir end |
.strip_color_codes(text) ⇒ String
Remove color codes from provided string. Color codes are of the format /(e[dd;ddm)+/.
410 411 412 |
# File 'lib/beaker/logger.rb', line 410 def Logger.strip_color_codes(text) text.gsub(/(\e|\^\[)\[(\d*;)*\d*m/, '') end |
Instance Method Details
#add_destination(dest) ⇒ Object
Construct an array of open steams for printing log messages to
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/beaker/logger.rb', line 135 def add_destination(dest) case dest when IO @destinations << dest when StringIO @destinations << dest when String @destinations << File.open(dest, 'w') else raise "Unsuitable log destination #{dest.inspect}" end end |
#color_host_output(*args) ⇒ Object
Custom reporting for messages generated by host SUTs - to preserve output Will not print unless we are at LOG_LEVELS ‘verbose’ or higher. Preserves outout by not stripping out colour codes
269 270 271 272 273 |
# File 'lib/beaker/logger.rb', line 269 def color_host_output *args return unless is_verbose? string = args.join optionally_color NONE, string, false end |
#convert(string) ⇒ Object
Remove invalid UTF-8 codes from provided string(s)
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/beaker/logger.rb', line 201 def convert string if string.kind_of?(Array) string.map do |s| convert s end else # Remove invalid and undefined UTF-8 character encodings string.to_s.force_encoding('UTF-8') return string.to_s.chars.select{|i| i.valid_encoding?}.join end end |
#debug(*args) ⇒ Object
Report a debug message. Will not print unless we are at LOG_LEVELS ‘debug’ or higher.
294 295 296 297 |
# File 'lib/beaker/logger.rb', line 294 def debug *args return unless is_verbose? optionally_color @log_colors[:debug], *args end |
#error(*args) ⇒ Object
Report an error message. Will always be reported.
335 336 337 |
# File 'lib/beaker/logger.rb', line 335 def error *args optionally_color @log_colors[:error], *args end |
#get_sublog ⇒ Object
Return the contents of the sublog
384 385 386 387 |
# File 'lib/beaker/logger.rb', line 384 def get_sublog @sublog.rewind @sublog.read end |
#host_output(*args) ⇒ Object
Custom reporting for messages generated by host SUTs. Will not print unless we are at LOG_LEVELS ‘verbose’ or higher. Strips any color codes already in the provided messages, then adds logger color codes before reporting
258 259 260 261 262 263 |
# File 'lib/beaker/logger.rb', line 258 def host_output *args return unless is_verbose? strings = strip_colors_from args string = strings.join optionally_color @log_colors[:host], string, false end |
#info(*args) ⇒ Object
Report an info message. Will not print unless we are at LOG_LEVELS ‘info’ or higher.
312 313 314 315 |
# File 'lib/beaker/logger.rb', line 312 def info *args return unless is_info? optionally_color @log_colors[:info], *args end |
#is_debug? ⇒ Boolean
Are we at LOG_LEVELS debug?
171 172 173 |
# File 'lib/beaker/logger.rb', line 171 def is_debug? LOG_LEVELS[@log_level] >= LOG_LEVELS[:debug] end |
#is_info? ⇒ Boolean
Are we at LOG_LEVELS info?
189 190 191 |
# File 'lib/beaker/logger.rb', line 189 def is_info? LOG_LEVELS[@log_level] >= LOG_LEVELS[:info] end |
#is_notify? ⇒ Boolean
Are we at LOG_LEVELS notify?
195 196 197 |
# File 'lib/beaker/logger.rb', line 195 def is_notify? LOG_LEVELS[@log_level] >= LOG_LEVELS[:notify] end |
#is_trace? ⇒ Boolean
Are we at LOG_LEVELS trace?
165 166 167 |
# File 'lib/beaker/logger.rb', line 165 def is_trace? LOG_LEVELS[@log_level] >= LOG_LEVELS[:trace] end |
#is_verbose? ⇒ Boolean
Are we at LOG_LEVELS verbose?
177 178 179 |
# File 'lib/beaker/logger.rb', line 177 def is_verbose? LOG_LEVELS[@log_level] >= LOG_LEVELS[:verbose] end |
#is_warn? ⇒ Boolean
Are we at LOG_LEVELS warn?
183 184 185 |
# File 'lib/beaker/logger.rb', line 183 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.
327 328 329 330 |
# File 'lib/beaker/logger.rb', line 327 def notify *args return unless is_notify? optionally_color @log_colors[:notify], *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
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/beaker/logger.rb', line 352 def optionally_color color_code, msg, add_newline = true print_statement = add_newline ? :puts : :print msg = convert(msg) msg = prefix_log_line(msg) @destinations.each do |to| to.print color_code if @color to.send print_statement, msg to.print NORMAL if @color unless color_code == NONE end end |
#perf_output(*args) ⇒ Object
Custom reporting for performance/sysstat messages Will not print unless we are at LOG_LEVELS ‘debug’ or higher.
278 279 280 281 |
# File 'lib/beaker/logger.rb', line 278 def perf_output *args return unless is_debug? optionally_color @log_colors[:perf], *args end |
#prefix_log_line(line) ⇒ String
Prefixes a log line with the appropriate amount of whitespace for the level of test that’s running.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/beaker/logger.rb', line 219 def prefix_log_line line if line.kind_of?(Array) line.map do |s| prefix_log_line s end else line.gsub!(/\r/, '') has_ending_newline = line.end_with?("\n") actual_lines = line.split("\n") actual_lines.map! do |actual_line| @line_prefix + actual_line end new_line = actual_lines.join("\n") new_line << "\n" if has_ending_newline new_line 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
369 370 371 372 |
# File 'lib/beaker/logger.rb', line 369 def pretty_backtrace backtrace = caller(1) trace = is_debug? ? backtrace : purge_harness_files_from( backtrace ) ( trace ).join "\n" end |
#quiet(off = true) ⇒ Object
Turn on/off STDOUT logging
124 125 126 127 128 129 130 131 |
# File 'lib/beaker/logger.rb', line 124 def quiet(off = true) if off remove_destination(STDOUT) #turn off the noise! else remove_destination(STDOUT) #in case we are calling this in error and we are already noisy add_destination(STDOUT) end end |
#remove_destination(dest) ⇒ Object
Remove a steam from the destinations array based upon it’s name or file path
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/beaker/logger.rb', line 150 def remove_destination(dest) case dest when IO @destinations.delete(dest) when StringIO @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 |
#start_sublog ⇒ Object
Create a new StringIO log to track the current output
375 376 377 378 379 380 381 |
# File 'lib/beaker/logger.rb', line 375 def start_sublog if @sublog remove_destination(@sublog) end @sublog = StringIO.new add_destination(@sublog) end |
#step_in ⇒ Object
Sets the step level appropriately for logging to be indented correctly
240 241 242 243 |
# File 'lib/beaker/logger.rb', line 240 def step_in @line_prefix_length += 2 @line_prefix = ' ' * @line_prefix_length end |
#step_out ⇒ Object
Sets the step level appropriately for logging to be indented correctly
248 249 250 251 252 |
# File 'lib/beaker/logger.rb', line 248 def step_out @line_prefix_length -= 2 @line_prefix_length = 0 if @line_prefix_length < 0 @line_prefix = ' ' * @line_prefix_length end |
#strip_colors_from(lines) ⇒ Array<String>
Strip any color codes from provided string(s)
342 343 344 345 346 |
# File 'lib/beaker/logger.rb', line 342 def strip_colors_from lines Array( lines ).map do |line| Logger.strip_color_codes(convert(line)) end end |
#success(*args) ⇒ Object
Report a success message. Will always be reported.
320 321 322 |
# File 'lib/beaker/logger.rb', line 320 def success *args optionally_color @log_colors[:success], *args end |
#trace(*args) ⇒ Object
Report a trace message. Will not print unless we are at LOG_LEVELS ‘trace’ or higher.
286 287 288 289 |
# File 'lib/beaker/logger.rb', line 286 def trace *args return unless is_trace? optionally_color @log_colors[:trace], *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: ”.
303 304 305 306 307 |
# File 'lib/beaker/logger.rb', line 303 def warn *args return unless is_warn? strings = args.map {|msg| "Warning: #{msg}" } optionally_color @log_colors[:warn], strings end |