我正在为我的工作环境编写一个小型日志嗅探器程序,该程序在日志中搜索几个关键词,让用户知道他们有一些东西要查看。这部分非常简单,但我试图实现的功能之一是用户可以选择他们今天想要走多远的表单,这样他们就不会遇到几个月前的错误,这不再重要.
我有日期,我有从现在开始的差距和用户指定的范围。我只是不确定如何使范围和匹配工作。这是我的代码:
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
我不关心时间戳,只关心日期戳。
谢谢,如果你能帮忙。