Class: Semlogger::Tailer

Inherits:
Rails::Rack::LogTailer
  • Object
show all
Defined in:
lib/semlogger/tailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, log = nil) ⇒ Tailer

Returns a new instance of Tailer.



2
3
4
5
6
7
# File 'lib/semlogger/tailer.rb', line 2

def initialize app, log = nil
	@app = app
	log ||= Rails.root.join( 'log', Rails.env).to_s.gsub('%', '%%') + '.%Y-%m-%d.%$.log'
	@path = Pathname.new( log).cleanpath.to_s
	@files = {}
end

Instance Method Details

#dirscan(path, time) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/semlogger/tailer.rb', line 9

def dirscan path, time
	path = path.gsub /(%.)/ do |f|
		case f
		when '%%' then '%%'
		when '%Y' then yesterday.year
		when '%m' then yesterday.month
		when '%d' then yesterday.day
		when /%./ then '*'
		end
	end
	preday = time - 1.day
	Dir[ files].each do |file|
		@files[file] ||= [::File.open( file, 'r'), 0, preday]
	end
end

#tail!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/semlogger/tailer.rb', line 25

def tail!
	yesterday, today = 1.day.ago.beginning_of_day, Time.now.beginning_of_day
	dirscan @path, yesterday
	dirscan @path, today

	@files.each do |fn, meta|
		file = meta[0]
		file.seek meta[1]
		unless file.eof?
			contents = file.read
			cursor = file.tell
			$stdout.print contents
		end
		meta[2] = today  if meta[1] == cursor
		meta[1] = cursor
		if yesterday > meta[2]
			meta[0].close
			@files.delete fn
		end
	end
end