From: Brian Scott Date: 2007-06-21T14:29:19+09:00 Subject: Re: Log file doesn't show message unsubscribe > ----- Original Message ----- > From: "Florian A�mann" > To: ruby-talk@ruby-lang.org > Subject: Re: Log file doesn't show message > Date: Fri, 15 Jun 2007 18:00:03 +0900 > > > Hello Tom, > > I'm not sure what this code should do when it's finished but I > rewrote the src, hope this helps: > > # Initialize Logger > require 'logger' > root_logger = Logger.new 'root.log' > root_logger.level = Logger::INFO > > # Setup at_exit hook > at_exit { root_logger.info 'done.' } > > %w{ rubygems hpricot open-uri }.each { |lib| require lib } > URL_MASK = 'http://www.securityfocus.com/bid/%i'.freeze > > tgroup = ThreadGroup.new > > root_logger.info 'Parsing SecurityFocus...' > ( 1 .. 3 ).each do |id| > root_logger.info " open #{ URL_MASK % id }" > thread = Thread.new do > begin > Hpricot open( URL_MASK % id ) > rescue => error > root_logger.error error.inspect > end > end > tgroup.add thread > end > > tgroup.list.each_with_index do |thread, index| > if doc = thread.value > File.open( "#{ index }.txt", 'w+' ) do |file| > file << ( doc/'#vulnerability' ).inner_text > end > end > end > > Sincerely > Florian > > Am 15.06.2007 um 08:34 schrieb Tom Bombadil: > > > Hello, > > > > I'd like to have a logging function which either tells me that the text > > files have been created successfully or that provides me an error message > > should there have been a fatal error. In doing so I coded the following. > > Note, however, that a logfile is created but neither of the comments > > mentioned above are shown. Why? > > > > > > # loads the library modules into the Ruby program > > require 'rubygems' > > require 'hpricot' > > require 'open-uri' > > require 'logger' > > > > # create a log file > > def do_at_exit(message,exit_code,logger) > > at_exit { logger.fatal(message) if exit_code == 1 } > > Process.exit(exit_code) > This would exit the Process (your programm) immediatly after > calling do_at_exit... > > > end > > > > logger = Logger.new('logfile.log') > > > > # logger formats: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN > > logger.level = Logger::INFO > > do_at_exit("Text files successfully created.\n",1,logger) if Process.uid != > > > > > > # creates array object and iterates over the collection passing elements one > > at a time to the "{ |e| ... }" block > > %w(rubygems hpricot open-uri).each { |e| require e } > You already requiered these (?) > > > > > # check new entries > > > > # Hpricot() method takes an object and loads the contents into a document > > object > > (1..3).each do |id| > > doc = Hpricot(open("http://www.securityfocus.com/bid/#{id}")) > > > > # opens a txt-file for each id according to aModeString (w:= write only, > > truncates existing file to zero > > # length or creates a new file for writing) and returns a new File object > > File.open("#{id}.txt", "w+") do |f| > > f << (doc/'#vulnerability').inner_text > > end > > > > end > = Official Barbell of USA Weight Lifting Great prices for training and certified barbells.guaranteed for life. http://a8-asy.a8ww.net/a8-ads/adftrclick?redirectid=437d7d29121ae82cbbe918b552f09c5a -- Powered by Outblaze