Object
Use Auto Logging block, RBatch automatically write to logfile.
Log file default location is "../log/YYYYMMDD_HHMMSS_${PROG_NAME}.log" .
If exception occuerd, then RBatch write stack trace to logfile.
script : ./bin/sample1.rb
require 'rbatch' RBatch::Log.new(){ |log| # Logging block log.info "info string" log.error "error string" raise "exception" }
logfile : ./log/20121020_005953_sample1.log
# Logfile created on 2012-10-20 00:59:53 +0900 by logger.rb/25413 I, [2012-10-20T00:59:53.895528 #3208] INFO -- : info string E, [2012-10-20T00:59:53.895582 #3208] ERROR -- : error string F, [2012-10-20T00:59:53.895629 #3208] FATAL -- : Caught exception; existing 1 F, [2012-10-20T00:59:53.895667 #3208] FATAL -- : exception (RuntimeError) test.rb:6:in `block in <main>' /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize' test.rb:3:in `new' test.rb:3:in `<main>'
Auto Logging Block.
opt = Option hash object. Hash keys is follows.
:name (String) = log file name. Default is "<date>_<time>_<prog>.log"
:dir (String) = log direcotry path. Default is "../log"
:level (String) = log level. ["debug"|"info"|"warn"|"error"|"fatal"] . Default is "info".
:append (Boolean) = appned to log or not(=overwrite). Default is ture.
:stdout (Boolean) = print string both logfile and STDOUT. Default is false.
:quiet (Boolean) = run quiet mode. print STDOUT nothing. Default is true.
log = Instance of Logger
RBatch::Log.new({:dir => "/tmp", :level => "info"}){ |log| log.info "info string" }
# File lib/rbatch/log.rb, line 91 def initialize(opt = nil) # parse option @opt = @@def_opt.clone @@def_opt.each_key do |key| if opt != nil && opt[key] != nil # use argument @opt[key] = opt[key] elsif RBatch.common_config != nil && RBatch.common_config["log_" + key.to_s] != nil # use config @opt[key] = RBatch.common_config["log_" + key.to_s] else # use default end end puts "option = " + @opt.to_s if @@verbose # determine log file name @prog_base = Pathname(File.basename(RBatch.program_name)).sub_ext("").to_s @file_name = @opt[:name].clone @file_name.gsub!("<date>", Time.now.strftime("%Y%m%d")) @file_name.gsub!("<time>", Time.now.strftime("%H%M%S")) @file_name.gsub!("<prog>", @prog_base) path = File.join(@opt[:dir],@file_name) # create Logger instance begin if @opt[:append] && File.exist?(path) @log = Logger.new(open(path,"a")) else @log = Logger.new(open(path,"w")) end rescue Errno::ENOENT => e STDERR.puts "RBatch ERROR: Can not open log file - #{path}" if ! @opt[:quiet] raise e end # set logger option formatter = proc do |severity, datetime, progname, msg| head = "[#{datetime}] [" + sprintf("%-5s",severity) +"]" if msg.is_a? Exception "#{head} #{msg}\n" + msg.backtrace.map{|s| " [backtrace] #{s}"}.join("\n") + "\n" else "#{head} #{msg}\n" end end @log.formatter = @opt[:formatter] if @opt[:formatter] @log.level = @@log_level_map[@opt[:level]] @log.formatter = formatter if @opt[:stdout] # ccreate Logger instance for STDOUT @stdout_log = Logger.new(STDOUT) @stdout_log.formatter = @opt[:formatter] if @opt[:formatter] @stdout_log.level = @@log_level_map[@opt[:level]] @stdout_log.formatter = formatter end puts "Log file: " + path if ! @opt[:quiet] # delete old log self.delete_old_log(@opt[:delete_old_log_date]) if @opt[:delete_old_log] # Start logging self.info("Start Logging. (PID=#{$$.to_s})") if ! @opt[:quiet] if block_given? begin yield self rescue => e self.fatal(e) self.fatal("Caught exception. Exit 1") exit 1 ensure self.close end end end
# File lib/rbatch/log.rb, line 187 def close @stdout_log.close if @opt[:stdout] @log.close end
# File lib/rbatch/log.rb, line 182 def debug(a) @stdout_log.debug(a) if @opt[:stdout] @log.debug(a) end
Delete old log files. If @opt is not include “<date>”, then do nothing.
date (Integer): The day of leaving log files
# File lib/rbatch/log.rb, line 198 def delete_old_log(date = 7) if Dir.exists?(@opt[:dir]) && @opt[:name].include?("<date>") Dir::foreach(@opt[:dir]) do |file| r = Regexp.new("^" + @opt[:name].gsub("<prog>",@prog_base) .gsub("<time>","[0-2][0-9][0-5][0-9][0-5][0-9]") .gsub("<date>","([0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9])") + "$") if r =~ file && Date.strptime($1,"%Y%m%d") <= Date.today - date puts "Delete old log file: " + File.join(@opt[:dir] , file) if ! @opt[:quiet] File::delete(File.join(@opt[:dir] , file)) end end end end
# File lib/rbatch/log.rb, line 167 def error(a) @stdout_log.error(a) if @opt[:stdout] @log.error(a) end
# File lib/rbatch/log.rb, line 162 def fatal(a) @stdout_log.fatal(a) if @opt[:stdout] @log.fatal(a) end
Generated with the Darkfish Rdoc Generator 2.