【问题标题】:How do I time stamp a logfile with Rake?如何使用 Rake 为日志文件添加时间戳?
【发布时间】: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.

另外,我是否有效地使用了filedirectory 方法?有没有更好的办法?

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

【问题讨论】:

    标签: ruby datetime rake


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 2016-08-16
      • 2012-05-03
      • 1970-01-01
      • 2015-08-30
      • 2012-12-24
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      相关资源
      最近更新 更多