调整了注释

This commit is contained in:
lizanle 2015-01-27 14:21:51 +08:00
parent 5afbf6dbca
commit d8b5252279
1 changed files with 150 additions and 127 deletions

View File

@ -1,7 +1,12 @@
# Time 2015-01-26 17:30:16
# Author lizanle
# Description 日志帮助类
module SystemLogHelper module SystemLogHelper
class SystemLog class SystemLog
class << self class << self
# 分页(支持多关键字查询) # Time 2015-01-26 17:29:17
# Author lizanle
# Description 分页(支持多关键字查询)
def logo_data(page, per, search, day) def logo_data(page, per, search, day)
logs = find_all_logs day logs = find_all_logs day
if logs.empty? #如果返回的是空數組,就說明日誌文件不存在 if logs.empty? #如果返回的是空數組,就說明日誌文件不存在
@ -29,7 +34,9 @@ module SystemLogHelper
logs logs
end end
#清除日誌 # Time 2015-01-26 17:28:57
# Author lizanle
# Description 清除日誌
def clear day def clear day
if File::exists?(logfile_path day) if File::exists?(logfile_path day)
File.open(logfile_path(day), 'w') do |f| File.open(logfile_path(day), 'w') do |f|
@ -39,7 +46,9 @@ module SystemLogHelper
end end
end end
#讀取日誌 # Time 2015-01-26 17:28:49
# Author lizanle
# Description 讀取日誌
private private
def find_all_logs day def find_all_logs day
if File::exists?(logfile_path day) if File::exists?(logfile_path day)
@ -52,7 +61,9 @@ module SystemLogHelper
end end
end end
# 日志文件的路径一般在Rails.root/log下根据环境配置 # Time 2015-01-26 17:28:34
# Author lizanle
# Description 日志文件的路径一般在Rails.root/log下根据环境配置
# 依次记录到product.log development.log test.log中 # 依次记录到product.log development.log test.log中
def logfile_path day def logfile_path day
#将日期处理成2015-01-01的形式 #将日期处理成2015-01-01的形式
@ -74,33 +85,42 @@ module SystemLogHelper
end end
end end
#替換換行符 # Time 2015-01-26 17:28:22
# Author lizanle
# Description 替換換行符
def parse(log) def parse(log)
ERB::Util.html_escape(log.gsub(/\e\[[\d;m]+/, '')).gsub("\n", "<br/>") ERB::Util.html_escape(log.gsub(/\e\[[\d;m]+/, '')).gsub("\n", "<br/>")
end end
#定义响应正则表达式 2015-01-20 11:31:13 INFO -- Completed 200 OK in 125ms (Views: 81.0ms | ActiveRecord: 2.0ms) # Time 2015-01-26 17:28:07
# Author lizanle
# Description 定义响应正则表达式 2015-01-20 11:31:13 INFO -- Completed 200 OK in 125ms (Views: 81.0ms | ActiveRecord: 2.0ms)
def response_regex def response_regex
'Completed \d+ \w+ in (\d+)ms \(Views: (\d+\.\d+)?ms \| ActiveRecord: (\d+\.\d+)?ms\)' 'Completed \d+ \w+ in (\d+)ms \(Views: (\d+\.\d+)?ms \| ActiveRecord: (\d+\.\d+)?ms\)'
end end
#将一条记录中的地址主机等都分析出来 # Time 2015-01-26 17:27:51
# Author lizanle
# Description 将一条记录中的地址主机等都分析出来
def get_status(paragraph) def get_status(paragraph)
request_regex = 'Started GET \"(\/.*)\" for ([\d]+\.[\d]+\.[\d]+\.[\d]+) at [\d]*-([\d]*-[\d]* [\d]*:[\d]*:[\d]*)' request_regex = 'Started GET \"(\/.*)\" for ([\d]+\.[\d]+\.[\d]+\.[\d]+) at [\d]*-([\d]*-[\d]* [\d]*:[\d]*:[\d]*)'
controller_regex = 'Processing by ([\w]+#[\w]+)' controller_regex = 'Processing by ([\w]+#[\w]+)'
page_time_regex = 'Views: \d+(\.\d+)?ms' page_time_regex = 'Views: \d+(\.\d+)?ms'
activeRecord_time_regex = 'ActiveRecord: \d+(\.\d+)?ms' activeRecord_time_regex = 'ActiveRecord: \d+(\.\d+)?ms'
#解析请求中的正则,主机,时间
if paragraph.match(request_regex) != nil if paragraph.match(request_regex) != nil
request_url = paragraph.match(request_regex)[1] #正则表达式中的括号能够截取成数组 request_url = paragraph.match(request_regex)[1] #正则表达式中的括号能够截取成数组
request_host = paragraph.match(request_regex)[2] request_host = paragraph.match(request_regex)[2]
request_at = paragraph.match(request_regex)[3] request_at = paragraph.match(request_regex)[3]
end end
#解析控制器
if paragraph.match(controller_regex) != nil if paragraph.match(controller_regex) != nil
controller_name = paragraph.match(controller_regex)[1] controller_name = paragraph.match(controller_regex)[1]
end end
#解析响应时间以及计算百分比
if paragraph.match(response_regex) != nil if paragraph.match(response_regex) != nil
#print(paragraph.match(response_regex)) #print(paragraph.match(response_regex))
total_time = paragraph.match(response_regex)[1] total_time = paragraph.match(response_regex)[1]
@ -110,12 +130,15 @@ module SystemLogHelper
activeRecord_time_percent = activeRecord_time.to_f/(total_time.to_f) activeRecord_time_percent = activeRecord_time.to_f/(total_time.to_f)
else else
end end
#将解析结果当做一条记录数组返回
request_status = [request_url, request_host, request_at, request_status = [request_url, request_host, request_at,
controller_name, total_time, page_time, page_time_percent, activeRecord_time, activeRecord_time_percent] controller_name, total_time, page_time, page_time_percent, activeRecord_time, activeRecord_time_percent]
request_status request_status
end end
#分析日志 # Time 2015-01-26 16:41:51
# Author lizanle
# Description 分析日志
public public
def analysis day def analysis day
csv = Array.new csv = Array.new