Class: Ixtlan::RollingFile

Inherits:
Logging::Appenders::RollingFile
  • Object
show all
Defined in:
lib/ixtlan/rolling_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ RollingFile

Returns a new instance of RollingFile.



11
12
13
14
15
16
17
18
19
20
# File 'lib/ixtlan/rolling_file.rb', line 11

def initialize( name, opts = {} )
  @date_pattern = opts.getopt(:date_pattern, '%Y-%m-%d')
  @extension = opts.getopt(:filename_extension, 'log')
  @filename_base = opts.getopt(:filename_base, 'log')
  opts.delete(:age)
  opts[:truncate] = false
  opts[:filename] = current_logfile
  super(name, opts)
  roll_files
end

Instance Method Details

#current_logfileObject



7
8
9
# File 'lib/ixtlan/rolling_file.rb', line 7

def current_logfile
  "#{@filename_base}_#{Time.now.strftime(@date_pattern)}.#{@extension}"
end

#roll_filesObject



26
27
28
29
30
31
32
33
34
# File 'lib/ixtlan/rolling_file.rb', line 26

def roll_files
  @fn = current_logfile
  files = Dir.glob("#{@filename_base}_*.#{@extension}").sort
  if (files.size > @keep)
    files[0..(files.size - 1 - @keep)].each do |file|
      ::File.delete file
    end
  end
end

#roll_required?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ixtlan/rolling_file.rb', line 22

def roll_required?( str = nil )
  not ::File.exist?(current_logfile)
end

#write(event) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ixtlan/rolling_file.rb', line 36

def write( event )
  str = event.instance_of?(::Logging::LogEvent) ?
  @layout.format(event) : event.to_s
  return if str.empty?

  check_logfile

  if roll_required?(str)
    return roll unless @lockfile

    begin
      @lockfile.lock {
        check_logfile
        roll if roll_required?
      }
    rescue
      # just do it without lock !!
      check_logfile
      roll if roll_required?
    end
  end
  super(str)
end