I'm using rake to build my C++ project and I have a lot of dependencies and tasks.
I would like save log from each task to separate log file. Is it possible? I want save all stdout from task to file.
I can't to this like:
log = Logger.new
log.info sh "my command"
because sometimes I lost info from process because sometimes I run some subprocess, etc. I want all STDOUT but separated for every task.
Can I do this? I tried modify execure function from Task class in rake but without success.
You can use a Rake::Task#name to get the name of the current task to interpolate into the name of your logfile. You can also use the task name as the progname for your log entry.
A Rakefile can also contain arbitrary Ruby code in addition to standard Rake methods. You can use this to create a custom logging method that allows you to create a log per Rake task, with your shell command's standard output captured using the Kernel#` method and passed along to the custom logging method as the log message.
require 'logger'
def log task, msg
l = Logger.new "#{task}.log"
l.progname = task
l.info msg
end
task :foo do |task|
msg = `echo Hello, world.`
log task.name, msg
end
task :bar do |task|
msg = `echo Goodbye, cruel world.`
log task.name, msg
end
task :default => [:foo, :bar]
$ rake; tail +1 *log
==> bar.log <==
# Logfile created on 2016-03-02 17:31:49 -0500 by logger.rb/53141
I, [2016-03-02T17:31:49.678729 #80846] INFO -- bar: Goodbye, cruel world.
==> foo.log <==
# Logfile created on 2016-03-02 17:31:49 -0500 by logger.rb/53141
I, [2016-03-02T17:31:49.677165 #80846] INFO -- foo: Hello, world.