Class: Tail::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
app/models/tail/log.rb

Constant Summary collapse

N_VALUE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



9
10
11
12
13
14
15
16
# File 'app/models/tail/log.rb', line 9

def initialize
  begin
    @n = N_VALUE
    @files = Dir.entries(Rails.root.join('log').to_s).select { |file| file.include? '.log' }
  rescue
    return []
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'app/models/tail/log.rb', line 5

def files
  @files
end

#nObject

Returns the value of attribute n.



6
7
8
# File 'app/models/tail/log.rb', line 6

def n
  @n
end

Instance Method Details

#flush(file_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/models/tail/log.rb', line 18

def flush(file_name)
  begin
    f = File.open "log/#{Rails.env}.log", 'w'
    f.close
      Rails.logger.warn "#{Rails.env}.log flushed"
  rescue => e
    Rails.logger.error(e.message)
    Rails.logger.error(e.backtrace[0..3].join("\n"))
  end
end

#tail(file_name) ⇒ log entries array

Parameters:

  • file_name (File name)
  • n (strings to return)

Returns:

  • (log entries array)


40
41
42
43
44
45
46
# File 'app/models/tail/log.rb', line 40

def tail(file_name)
  begin
    @files.include?(file_name) ? `tail -n #{@n} log/#{file_name}`.lines : []
  rescue
    return []
  end
end