【发布时间】:2017-04-10 21:24:33
【问题描述】:
在 Rake 任务中持续为日志文件添加时间戳的简单、清晰和简洁的方法是什么?
我看到有一个#timestamp 方法;但是,在阅读了它的描述后,我仍然不确定如何使用它:
timestamp()
Time stamp for file creation task.
This time stamp is earlier than any other time stamp.
另外,我是否有效地使用了file 和directory 方法?有没有更好的办法?
require 'time'
task :default => [:timestamp]
directory 'some/path/to'
file 'some/path/to/some.log' => ['some/path/to']
task :timestamp => ['some/path/to/some.log'] do
File.open('some/path/to/some.log', 'a') do |f|
f.puts Time.now.iso8601
end
end
用法:
$ cat some/path/to/some.log
2017-04-10T14:16:44-07:00
2017-04-10T14:16:55-07:00
$ rake
$ cat some/path/to/some.log
2017-04-10T14:16:44-07:00
2017-04-10T14:16:55-07:00
2017-04-10T14:21:14-07:00
【问题讨论】: