Class: LogPacker::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/log_packer/packer.rb

Class Method Summary collapse

Class Method Details

.archive_path(logfile) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/log_packer/packer.rb', line 12

def archive_path(logfile)
  archived_dir = File.dirname(logfile) + '/archived'
  last_dir = File.dirname(logfile) + '/last'
  FileUtils.mkdir(archived_dir) unless File.directory?(archived_dir)
  FileUtils.mkdir(last_dir) unless File.directory?(last_dir)
  archived_dir + '/' + File.basename(logfile) + '.' + Time.now.strftime('%Y%m%d%H%M%S')
end

.last_dir(logfile) ⇒ Object



20
21
22
23
24
25
# File 'lib/log_packer/packer.rb', line 20

def last_dir(logfile)
  log_path = File.directory?(logfile) ? logfile : File.dirname(logfile)
  last_dir = log_path + '/last/'
  FileUtils.mkdir(last_dir) unless File.directory?(last_dir)
  last_dir
end

.rotate(logfile) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/log_packer/packer.rb', line 27

def rotate(logfile)
  if !File.exists?(logfile)
    puts "#{logfile} does not exist"
  elsif File.size(logfile) == 0
    puts "#{logfile} is empty"
  else
    archive_file = archive_path(logfile)
    last_dir = last_dir(logfile)
    puts "Copying #{logfile} to #{archive_file}.bz2"
    FileUtil.cp logfile, "#{archive_file}"
    truncate logfile
    system "bzip2 #{archive_file}"
    system "cd #{last_dir} && ln -sf #{archive_file}.bz2 ."
  end
end

.truncate(file) ⇒ Object

Functions



8
9
10
# File 'lib/log_packer/packer.rb', line 8

def truncate(file)
  `echo '' > #{file}`
end