Logging with rake


#1

Hi list,

Howto capture stdout + stderr from jobs invoked with rake?

I run rake from cron. I have a

def log(msg)
str = Time.now.to_s + ": " + msg + “\n”
file = $settings[‘logfile’]
File.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 S.

desc “creates a hotcopy backup of the repository.”
task :backup => [:clean] do
ok = true

log(‘create hotcopy’)
tmpdir = $settings[‘backup_tmpdir_name’]
repo_path = $settings[‘backup_repository_path’]
create_hotcopy(repo_path, tmpdir)

log(‘compressing’)
zipfile = $settings[‘backup_zipfile’]
compress_dir(tmpdir, zipfile)

log(‘encrypting’)
passphrase = $settings[‘passphrase’]
cryptfile = $settings[‘backup_cryptfile’]
encrypt_file(zipfile, cryptfile, passphrase)

log(‘splitting into chunks’)

split file into small chunks (that can go with the mail)

prefix = $settings[‘backup_chunk_prefix’]
size = $settings[‘backup_chunk_size’]
split_file(cryptfile, size, prefix)

send a mail with each chunk attached

rev = youngest_revision(tmpdir)
time = Time.now.strftime(’%Y%m%d’)
subject = “hotcopy#{time}_rev#{rev}”
reciever = $settings[‘backup_recievers’]
mime = ‘application/octet-stream’
chunks = 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 = “this is chunk##{index+1} out of #{chunks.size} in
total.”

attachments = [[filename, mime]]
begin
  send_mail(reciever, subject, bodytext, attachments)
	rescue => e
		log("ERROR: failed sending, #{e.inspect}")
		ok = false
	end

end
msg = ok ? “OK” : “with error!”
log(“backup completed #{msg}\n\n”)
end


#2

On Jan 26, 2006, at 4:11 PM, Simon S. wrote:

Hi list,

Howto capture stdout + stderr from jobs invoked with rake?

Well first, have you tried using IO redirection in a shell script and
run that from cron? The other option I would try would be to re-
assign STDOUT, $stdout and STDERR, $stderr at the beginning of your
script.


#3

From: Simon S. removed_email_address@domain.invalid
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 manytail -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.


#4

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’


#5

On 1/27/06, rubikitch removed_email_address@domain.invalid wrote:

From: Simon S. removed_email_address@domain.invalid

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

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 :wink:

And tail -f /log/handybackup.log' will help you. I use GNU Screen and have manytail -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 => [: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.


#6

rubikitch wrote:

From: Simon S. removed_email_address@domain.invalid
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

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

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

Guillaume.


#7

On 29/01/06, Simon S. removed_email_address@domain.invalid wrote:

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


#8

On 1/26/06, Simon S. removed_email_address@domain.invalid wrote:
[snip]

Howto capture stdout + stderr from jobs invoked with rake?
[snip]

On OpenBSD 3.7 the stdout is not syncronized.
but the stderr is.

however on OSX both is syncronized.

How to sync stdout?

I invoke rake like this:

#!/usr/local/bin/ruby
rake 2> lerr >> lout
puts “DONE”


Simon S.

(sorry for the long delay… I took a oneweek course in generative
programming :slight_smile: