1

我正在为我的工作环境编写一个小型日志嗅探器程序,该程序在日志中搜索几个关键词,让用户知道他们有一些东西要查看。这部分非常简单,但我试图实现的功能之一是用户可以选择他们今天想要走多远的表单,这样他们就不会遇到几个月前的错误,这不再重要.

我有日期,我有从现在开始的差距和用户指定的范围。我只是不确定如何使范围和匹配工作。这是我的代码:

require 'date'
### Get the logfile path from the user
p "What is the path to your log file?"
logfile = gets.chomp

### Get number of days user would like to go back
p "How many days back from today would you like to look?"
num_days = gets.chomp


############################################################################################
### Define Log class. Accept argumnet for path of the file. Called by foo = Log.new
############################################################################################
class Log

    def scan_log(file_name, days)
        error_exists = false
        verify = false
        apn = false
        strict = false
        days = days.to_i
        # time = Time.now.strftime("%Y-%m-%d")
        now = Date.today
        days_ago = (now - days)
        p now
        p days_ago

        File.open(file_name, "r") do |file_handle|
            file_handle.each_line do |line|
                if line =~ /Unable to verify signature/
                    verify = true
                    error_exists = true
                end

                if line =~ /gateway.push.apple.com/
                    apn = true
                    error_exists = true
                end

                if line =~ /Data truncation/
                    strict = true
                    error_exists = true
                end
            end
        end

        p "Verify signature error found" if verify


        p "You have an APNS error" if apn


        p "You have strict mode enabled" if strict

    end

end

l = Log.new
l.scan_log(logfile, num_days)

我的想法是 file_handle.each_line do... 下的循环会起作用。我会在循环中包含现有的 if 语句,并且循环会将用户设置的日期范围与日志中的日期相匹配。日志中的格式为:

2013-04-17 15:10:42, 767

我不关心时间戳,只关心日期戳。

谢谢,如果你能帮忙。

4

2 回答 2

1

解析自由格式日期的一种可靠方法是:

 > Time.parse(" 2013-04-17 13:15:24 -0700 ").strftime("%Y-%m-%d")
  => "2013-04-17" 
 > Time.parse("Wed Apr 17 13:15:09 PDT 2013 ").strftime("%Y-%m-%d")
  => "2013-04-17" 

假设用户以“%Y-%m-%d”格式指定 start_date 和时间范围,您可以这样做:

 start_date = Time.parse( user_provided_date ) # e.g. "2013-04-10"
 log_date = Time.parse( line )

 do_something if (start_date - log_date) < number_of_seconds  # or whatever time range

如果您只想显示比给定时间范围新的所有日志,那么您可以这样做:

 log_date = Time.parse( line )

 do_something if (Time.now - log_date) < number_of_seconds  # or whatever time range

要获得 Rails 风格的漂亮时间范围,例如 1.day 或 12.hours ,您可以包括ActiveSupport::Duration

于 2013-04-17T20:20:20.943 回答
0

您可以从日期构造一个范围(如问题标题所示)并使用它的 include?方法。

require 'date'

puts "How many days back from today would you like to look?"
days_back = gets.to_i #input: 3

logline_date = "2013-04-17 15:10:42, 767"

end_date = Date.today
start_date = end_date - days_back
selected_range = start_date..end_date

p selected_range.include?( Date.parse( logline_date )) #true (today for me, that is)
于 2013-04-17T21:05:45.723 回答