我正在爲我的工作環境編寫一個小型日誌嗅探器程序,用於在日誌中搜索幾個關鍵詞,讓用戶知道他們有一些要看的東西。這部分非常簡單,但我試圖實現的功能之一是用戶選擇他們想要返回多遠形式的選項,所以他們沒有從幾個月前得到錯誤,不再重要。Ruby日期範圍和匹配
我已經得到了日期,而且我從現在開始和用戶指定的範圍有差距。我只是不確定如何獲得範圍和匹配的工作。這裏是我的代碼:
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下的循環會...工作。我會在循環中包含現有的if語句,並且循環會將用戶設置的日期範圍與日誌中的日期相匹配。在日誌中的格式爲:
2013年4月17日十五點10分42秒,767
我不關心的時間戳,只是郵戳。
如果你可以幫忙,謝謝。
謝謝。我也會給這個鏡頭。上面的固定工作流程大部分都在工作,但當遇到沒有時間戳的行時,我遇到了Time.parse的問題。我得弄清楚現在。 – ckbrumb