# Log file doesn't show message

**URL:** <https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910>\
**Category:** Ruby\
**Created:** [June 15, 2007, 8:34am UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910 "2007-06-15T08:34:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 8:34am UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/1 "2007-06-15T08:34:40Z")

</div>

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)  
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  
!=  
0

# 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 }

# 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}](http://www.securityfocus.com/bid/#%7Bid%7D)”))

# 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

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 11:01am UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/2 "2007-06-15T11:01:05Z")

</div>

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](http://www.securityfocus.com/bid/%25i)’.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 B.:

> Process.exit(exit\_code)  
> This would exit the Process (your programm) immediatly after calling  
> do\_at\_exit…

> # 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 (?)

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 11:27am UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/3 "2007-06-15T11:27:12Z")

</div>

Great, thanks a lot! However, I just encountered a little bug → I’d  
like to  
have the bid on [www.securityfocus.com](http://www.securityfocus.com) as file name. Thus, for bugtraq id  
1  
 → 1.txt, for bugtraq id 2 = 2.txt, etc… How can I do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/4 "2007-06-15T13:13:38Z")

</div>

Not that easy I’d say as I now have Bugtraq ID: 5, but 1.txt 🙂

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/5 "2007-06-15T13:39:41Z")

</div>

Florian,

What if I’d only like to get the latest entries on  
[www.securityfocus.com/bid](http://www.securityfocus.com/bid)?  
I’d then need a check if #{ URL\_MASK % id } == 0 or so, yes?

So long,  
Tom

–

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 11:49am UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/6 "2007-06-15T11:49:14Z")

</div>

Add 1 to index when you choose the filename…

Am 15.06.2007 um 11:26 schrieb Tom B.:

> On 6/15/07, Florian Aßmann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > root\_logger.info " open #{ URL\_MASK % id }"  
> > tgroup.list.each\_with\_index do |thread, index|  
> > if doc = thread.value
> > 
> > # File.open( “#{ index }.txt”, ‘w+’ ) do |file|

File.open( “#{ index + 1 }.txt”, ‘w+’ ) do |file| # EASY ISN’T IT?

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/7 "2007-06-15T14:05:45Z")

</div>

Tom B. schrieb:

> What if I’d only like to get the latest entries on  
> [www.securityfocus.com/bid](http://www.securityfocus.com/bid)?  
> I’d subscribe the maillist or use a webservice if it exists, otherwise  
> you’d have to parse the html source of [www.securityfocus.com/bid](http://www.securityfocus.com/bid) and  
> find the first field with the bid…  
> Fetching the site is very expensive, takes much time and ressources.

> I’d then need a check if #{ URL\_MASK % id } == 0 or so, yes?  
> No, ‘URL\_MASK % id’ is like ‘sprintf URL\_MASK, id’, see above for  
> another solution.

Regards  
Florian

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [June 15, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/log-file-doesnt-show-message/106910/8 "2007-06-15T14:22:48Z")

</div>

All right. Lastly, it pretty much seems like it’s not that easy as I now  
get  
1.txt for the Bugtraq ID: 5 🙂 It should be 1-1, 2-2, etc.

Have a great weekend,  
-Tom

–
