Ruby 如何使用Rake对日志文件进行时间戳?

Ruby 如何使用Rake对日志文件进行时间戳?,ruby,datetime,rake,Ruby,Datetime,Rake,在Rake任务中,什么是一种简单、清晰、简洁的方法来连续地为日志文件添加时间戳 我知道有一种方法;然而,在阅读了它的描述之后,我仍然不知道如何使用它: timestamp() Time stamp for file creation task. This time stamp is earlier than any other time stamp. 另外,我是否有效地使用了文件和目录方法?有更好的办法吗 require 'time' task :default => [:times

在Rake任务中,什么是一种简单、清晰、简洁的方法来连续地为日志文件添加时间戳

我知道有一种方法;然而,在阅读了它的描述之后,我仍然不知道如何使用它:

timestamp()

Time stamp for file creation task.
This time stamp is earlier than any other time stamp.
另外,我是否有效地使用了
文件
目录
方法?有更好的办法吗

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

这个时间戳不是你需要的。Rake不支持您想要的内容。您可以将Debian中提供的“moreutils”包中的标准输出通过管道传输到“ts”工具,以添加时间戳

此方法用于处理文件时间戳