Class: FluentdLog

Inherits:
Object
  • Object
show all
Defined in:
app/models/fluentd_log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FluentdLog

Returns a new instance of FluentdLog.



4
5
6
# File 'app/models/fluentd_log.rb', line 4

def initialize(path)
  @log_file = path
end

Instance Attribute Details

#log_fileObject (readonly)

Returns the value of attribute log_file.



2
3
4
# File 'app/models/fluentd_log.rb', line 2

def log_file
  @log_file
end

Instance Method Details

#errors_since(since = 1.day.ago) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/models/fluentd_log.rb', line 14

def errors_since(since = 1.day.ago)
  errors = []
  logged_errors do |error|
    break if Time.parse(error[:subject]) < since
    errors << error
  end
  errors
end

#last_error_messageObject



32
33
34
# File 'app/models/fluentd_log.rb', line 32

def last_error_message
  recent_errors(1).first.try(:[], :subject) || ""
end

#readObject



8
9
10
11
12
# File 'app/models/fluentd_log.rb', line 8

def read
  return "" unless File.exists?(log_file)
  content = File.open(log_file, "r+:ascii-8bit"){|f| f.read } # TODO: large log file
  content.force_encoding('utf-8').valid_encoding? ? content : content.force_encoding('ascii-8bit')
end

#recent_errors(limit = 3) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/models/fluentd_log.rb', line 23

def recent_errors(limit = 3)
  errors = []
  logged_errors do |error|
    errors << error
    break if errors.length >= limit
  end
  errors
end

#tail(limit = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/fluentd_log.rb', line 36

def tail(limit = nil)
  return [] unless File.exists?(log_file)

  limit = limit.to_i rescue 0
  limit = limit.zero? ? Settings.default_log_tail_count : limit
  io = File.open(log_file)
  buf = []
  reader = ::FileReverseReader.new(io)
  reader.each_line do |line|
    buf << line
    break if buf.length >= limit
  end
  buf
end