日志查看Helper

This commit is contained in:
lizanle 2015-01-14 16:43:39 +08:00
parent 75ba61ed74
commit 2431fea63e
2 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,57 @@
module SystemLogHelper
class SystemLog
class << self
# 分页(支持多关键字查询)
def logo_data(page,per,search)
logs = find_all_logs
# 根据search参数来决定是否需要查询
keywords = search
if keywords && !keywords.strip.blank?
# 把keywords转化成正则表达式数组
keywords = keywords.strip.split(/\s+/).collect! {|w| Regexp.new(w, 'i')}
# 一条记录应该匹配每个关键字
logs = logs.find_all do |log|
keywords.all? { |r| log =~ r }
end
logs = Kaminari.paginate_array(logs).page(page).per(per).collect! {|log| parse(log)}
logs.collect! do |log|
keywords.each { |r| log.gsub!(r, '<span class="search_results">\0</span>')}
log
end
else
logs = Kaminari.paginate_array(logs).page(page).per(per).collect! {|log| parse(log)}
end
logs
end
#清除日誌
def clear
File.open(logfile_path, 'w') do |f|
f.print ''
end
end
#讀取日誌
private
def find_all_logs
File.open(logfile_path) do |f|
#打开文件并按照正则表达式切分逆序最新一个记录可以扔掉因为最新的记录永远都是访问System_log)
f.read.split("Processing").reverse[1..-1]
end
end
# 日志文件的路径一般在Rails.root/log下根据环境配置
# 依次记录到product.log development.log test.log中
def logfile_path
File.join(Rails.root, "log", "#{Rails.env}.log")
end
#替換換行符
def parse(log)
ERB::Util.html_escape(log.gsub(/\e\[[\d;m]+/, '')).gsub("\n", "<br/>")
end
end
end
end

View File

@ -81,7 +81,5 @@ class Document < ActiveRecord::Base
update_document(self.user,2,self.project) update_document(self.user,2,self.project)
end end
def expire_cache
ActionController::Base.new.expire_fragment('activities')
end
end end