Class: Lumberjack::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/device.rb,
lib/lumberjack/device/null.rb,
lib/lumberjack/device/writer.rb,
lib/lumberjack/device/log_file.rb,
lib/lumberjack/device/rolling_log_file.rb,
lib/lumberjack/device/date_rolling_log_file.rb,
lib/lumberjack/device/size_rolling_log_file.rb

Overview

This is an abstract class for logging devices. Subclasses must implement the write method and may implement the close and flush methods if applicable.

Direct Known Subclasses

Null, Writer

Defined Under Namespace

Classes: DateRollingLogFile, LogFile, Null, RollingLogFile, SizeRollingLogFile, Writer

Instance Method Summary collapse

Instance Method Details

#cleanup_files!Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/lumberjack/device/rolling_log_file.rb', line 83

def cleanup_files!
  if keep
    files = Dir.glob("#{path}.*").collect{|f| [f, File.ctime(f)]}.sort{|a,b| b.last <=> a.last}.collect{|a| a.first}
    if files.size > keep
      files[keep, files.length].each do |f|
        File.delete(f)
      end
    end
  end
end

#closeObject

Subclasses may implement this method to close the device.



18
19
20
# File 'lib/lumberjack/device.rb', line 18

def close
  flush
end

#do_once(file) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lumberjack/device/rolling_log_file.rb', line 94

def do_once(file)
  begin
    file.flock(File::LOCK_EX)
  rescue SystemCallError
    # Most likely can't lock file because the stream is closed
    return
  end
  begin
    verify = file.lstat rescue nil
    # Execute only if the file we locked is still the same one that needed to be rolled
    yield if verify && verify.ino == @file_inode && verify.size > 0
  ensure
    file.flock(File::LOCK_UN) rescue nil
  end
end

#flushObject

Subclasses may implement this method to flush any buffers used by the device.



23
24
# File 'lib/lumberjack/device.rb', line 23

def flush
end

#write(entry) ⇒ Object

Subclasses must implement this method to write a LogEntry.

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/lumberjack/device.rb', line 13

def write(entry)
  raise NotImplementedError
end