require "rubygems" require "thread" DELAY_TEMPLATES = { :daily => {:cmd_string => "cronolog ",:format_string => "\%Y/\%m/\%d"}, :hourly => {:cmd_string => "cronolog --delay '1 hour' ",:format_string => nil}, :midnight => {:cmd_string => "cronolog --delay '12 hours' ",:format_string => nil}} class Log4u # shifting_interval could be daily,weekly,hourly def initialize(_file_name,delay_interval = :daily) raise(ArgumentError.new,"Invalid arguments") unless [:daily,:hourly,:midnight].include? delay_interval @mutex = Mutex.new filename_string = File.basename(_file_name) dirname_string = File.dirname(_file_name) if (format_string = DELAY_TEMPLATES[delay_interval][:format_string]) option_string = "#{dirname_string}/#{format_string}/#{filename_string}" else option_string = "#{dirname_string}/#{filename_string}" end cmd_string = DELAY_TEMPLATES[delay_interval][:cmd_string] @log_pipe = IO.popen("#{cmd_string} #{option_string}","w") end def time_string time_now = Time.now time_now.strftime("%I:%M%p") end def error msg @mutex.lock @log_pipe.puts "ERROR: #{time_string}: #{msg}" @mutex.unlock end def info msg @mutex.lock @log_pipe.puts "INFO #{time_string}: #{msg}" @mutex.unlock end def debug msg @mutex.lock @log_pipe.puts "DEBUG #{time_string}: #{msg}" @mutex.unlock end def close_log @mutex.lock @log_pipe.close @mutex.unlock end end