logging with rake

S

Simon Strandgaard

Hi list,

Howto capture stdout + stderr from jobs invoked with rake?


I run rake from cron. I have a

def log(msg)
=09str =3D Time.now.to_s + ": " + msg + "\n"
=09file =3D $settings['logfile']
=09File.open(file, 'a+') {|f| f.write str }
end

it only tells where it went wrong, but not what went wrong.





any ideas how to do better logging with the following code?

--
Simon Strandgaard



desc "creates a hotcopy backup of the repository."
task :backup =3D> [:clean] do
ok =3D true

log('create hotcopy')
tmpdir =3D $settings['backup_tmpdir_name']
repo_path =3D $settings['backup_repository_path']
create_hotcopy(repo_path, tmpdir)

log('compressing')
zipfile =3D $settings['backup_zipfile']
compress_dir(tmpdir, zipfile)


log('encrypting')
passphrase =3D $settings['passphrase']
cryptfile =3D $settings['backup_cryptfile']
encrypt_file(zipfile, cryptfile, passphrase)


log('splitting into chunks')
# split file into small chunks (that can go with the mail)
prefix =3D $settings['backup_chunk_prefix']
size =3D $settings['backup_chunk_size']
split_file(cryptfile, size, prefix)


# send a mail with each chunk attached
rev =3D youngest_revision(tmpdir)
time =3D Time.now.strftime('%Y%m%d')
subject =3D "hotcopy#{time}_rev#{rev}"
reciever =3D $settings['backup_recievers']
mime =3D 'application/octet-stream'
chunks =3D Dir.glob(prefix + '*').sort
log("revision #{rev}, consists of #{chunks.size} chunks.")
chunks.each_with_index do |filename, index|
log("sending chunk##{index+1}.")
bodytext =3D "this is chunk##{index+1} out of #{chunks.size} in total."

attachments =3D [[filename, mime]]
begin
send_mail(reciever, subject, bodytext, attachments)
=09=09rescue =3D> e
=09=09=09log("ERROR: failed sending, #{e.inspect}")
=09=09=09ok =3D false
=09=09end
end
msg =3D ok ? "OK" : "with error!"
log("backup completed #{msg}\n\n")
end
 
R

rubikitch

From: Simon Strandgaard <[email protected]>
Subject: logging with rake
Date: Fri, 27 Jan 2006 06:11:43 +0900
Howto capture stdout + stderr from jobs invoked with rake?


I run rake from cron. I have a

cd /path/to/backup; rake 2>&1 >> /log/handybackup.log

And `tail -f /log/handybackup.log' will help you.
I use GNU Screen and have many `tail -f' windows in Screen.
def log(msg)
str = Time.now.to_s + ": " + msg + "\n"
file = $settings['logfile']
File.open(file, 'a+') {|f| f.write str }
end

Using shell redirection makes the log method simple.

def log(msg)
str = Time.now.to_s + ": " + msg + "\n"
print str
end

desc "creates a hotcopy backup of the repository."
task :backup => [:clean] do

I think the `backup' task shold be splitted into some small tasks.
 
S

Simon Strandgaard

cd /path/to/backup; rake 2>&1 >> /log/handybackup.log

Aha.. I did'nt knew redirection was possible.. very nice.


I had to put it in paranthesis for it to work.. maybe parenthesis is a
bash thing.

(rake 2>&1) >> log


anyways.. it works ;-)

And `tail -f /log/handybackup.log' will help you.
I use GNU Screen and have many `tail -f' windows in Screen.

GNU Screen is nice as well.. its been a while since I tried it out.
I wonder how to automaticly launch screen with a bunch of
'tail -f somelog'.. I have looked through some tutorials,
but they are mostly about what the keystrokes does.


desc "creates a hotcopy backup of the repository."
task :backup =3D> [:clean] do

I think the `backup' task shold be splitted into some small tasks.

Indeed its long.. I will split it. Thanks.




Sorry for the delay.
 
S

Simon Strandgaard

When invoking `cmd 2> log2 >> log1` within ruby..
is there any issues concerning the shell I should be aware of?
placement of parenthesis and stuff.

or will 2> and >> always work as long we are on unix?




prompt> ./a.rb
STDOUT:
stdout
STDERR:
stderr
DONE


prompt> cat a.rb
#!/usr/local/bin/ruby
`rm lout lerr`
`touch lout lerr`
`ruby test.rb 2> lerr >> lout`
puts "STDOUT:\n" + IO.read('lout')
puts "STDERR:\n" + IO.read('lerr')
puts "DONE"


prompt> cat test.rb
$stdout.puts 'stdout'
$stderr.puts 'stderr'
 
A

Austin Ziegler

When invoking `cmd 2> log2 >> log1` within ruby.. is there any issues
concerning the shell I should be aware of? placement of parenthesis
and stuff. or will 2> and >> always work as long we are on unix?

2>&1 and a few other Unix redirectionisms will work on Windows with
cmd.exe as well.

-austin
 
G

Guillaume Marcais

rubikitch said:
From: Simon Strandgaard <[email protected]>
Subject: logging with rake
Date: Fri, 27 Jan 2006 06:11:43 +0900


cd /path/to/backup; rake 2>&1 >> /log/handybackup.log

The order is significant. To append both stdout and stderr to the log
file, it should be:

rake >>/log/handybackup.log 2>&1

Guillaume.
And `tail -f /log/handybackup.log' will help you.
I use GNU Screen and have many `tail -f' windows in Screen.
def log(msg)
str = Time.now.to_s + ": " + msg + "\n"
file = $settings['logfile']
File.open(file, 'a+') {|f| f.write str }
end

Using shell redirection makes the log method simple.

def log(msg)
str = Time.now.to_s + ": " + msg + "\n"
print str
end

desc "creates a hotcopy backup of the repository."
task :backup => [:clean] do

I think the `backup' task shold be splitted into some small tasks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top