Forum: Ruby logging with rake

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 Simon Strandgaard (Guest)
on 2006-01-26 22:14
(Received via mailing list)
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 Strandgaard



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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-01-27 02:46
(Received via mailing list)
On Jan 26, 2006, at 4:11 PM, Simon Strandgaard 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.
15bd00b1919f3f2920dc70da19c1ead9?d=identicon&s=25 rubikitch (Guest)
on 2006-01-27 11:45
(Received via mailing list)
From: Simon Strandgaard <neoneye@gmail.com>
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.
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 Simon Strandgaard (Guest)
on 2006-01-29 12:22
(Received via mailing list)
On 1/27/06, rubikitch <rubikitch@ruby-lang.org> wrote:
> From: Simon Strandgaard <neoneye@gmail.com>
> > 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 ;-)


> 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 => [: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.
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 Simon Strandgaard (Guest)
on 2006-01-29 15:41
(Received via mailing list)
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'
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-01-29 16:27
(Received via mailing list)
On 29/01/06, Simon Strandgaard <neoneye@gmail.com> 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
D8831c4665a164c6ce484003deb1afd6?d=identicon&s=25 Guillaume Marcais (Guest)
on 2006-01-31 12:44
(Received via mailing list)
rubikitch wrote:
> From: Simon Strandgaard <neoneye@gmail.com>
> 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.
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 Simon Strandgaard (Guest)
on 2006-02-07 00:06
(Received via mailing list)
On 1/26/06, Simon Strandgaard <neoneye@gmail.com> 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 Strandgaard

(sorry for the long delay.. I took a oneweek course in generative
programming :-)
This topic is locked and can not be replied to.