Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Watir Logging

// setup logging infrastructure for Watir tests using ruby Logger class

#Logging infrastructure
require 'logger'
# hack ruby logger to format and output only the minimum needed for testing scripts
# Logger is configured to write 
class Logger
  # hack format of logger
  class Formatter
    #original: Format = "%s, [%s#%d] %5s -- %s: %s\n"
    #Change the name of constant to avoid redefining the super which raises exception and bugs us.
    Formato = "[%s] [%5s] : %s\n" 
    
    #keep the original signature but alter implementation to change formatting
    def call(severity, time, progname, msg)
      #add logging to stdout
      puts output_message = msg2str(msg)
      STDOUT.flush
      #original: Format % [severity[0..0], format_datetime(time), $$, severity, progname, msg2str(msg)]
      Formato % [format_datetime(time),severity, output_message]
    end
  end
end

logfile = File.join(File.dirname(__FILE__),'..','log', 'logger.log')
$log = Logger.new(logfile,'daily')
$log.level = Logger::DEBUG
$log.datetime_format = "%H:%M:%S"
$log.info("Watir Execution Starts")
$log.debug("Halllow Worled!")
$log.info("Achtung Achtung. The Train will leave from platform 9")
$log.warn("no no no no no. you can't do this")
$log.error("error error Danger Danger")
$log.fatal("game over game over game over")
# example of output
#[13:02:00] [DEBUG] : Watir Execution Starts
#[13:02:00] [DEBUG] : Halllow Worled!
#[13:02:00] [ INFO] : Achtung Achtung. The Train will leave from platform 9
#[13:02:00] [ WARN] : no no no no no. you can't do this
#[13:02:00] [ERROR] : error error Danger Danger
#[13:02:00] [FATAL] : game over game over game over


How to rotate your Rails logs by size, so you can allocate a certain amount of space for it.

Add this to RAILS_ROOT/config/environment.rb to keep 50 logfiles of 1MB each.
config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 50, 1048576)
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS