class StackifyRubyAPM::LogDevice

@api private

Constants

MAX_LOG_FILES_COUNT

Public Instance Methods

add_log_header(file) click to toggle source
# File lib/stackify_apm/logger/log_device.rb, line 23
def add_log_header(file)
  # Make it only empty.
end
create_logfile(filename) click to toggle source

Override create_logfile of core LogDevice class where we set File.chmod to 0o777

Calls superclass method
# File lib/stackify_apm/logger/log_device.rb, line 85
def create_logfile(filename)
  logdev = super
  File.chmod(0o777, filename)

  begin
    dir_name = File.dirname(filename)
    # repath current file due to windows separator \\ doesn't work with Dir.glob
    dir_name = dir_name.split(File::ALT_SEPARATOR).join(File::SEPARATOR)
    search_files = File.join("#{dir_name}", "{[!stackify-ruby-apm]*}.log")
    log_files = Dir.glob(search_files).sort_by { |f| File.stat(f).mtime}.reverse

    if log_files.length > MAX_LOG_FILES_COUNT
      files_to_delete = log_files[MAX_LOG_FILES_COUNT..-1]
      files_to_delete.each { |f|
        File.delete(f) if File.exists? f
      }
    end
  rescue
    # nothing to do here
  end

  logdev
end
shift_log_age() click to toggle source

rubocop:disable Style/RescueModifier rubocop:disable Lint/RescueWithoutErrorClass Newly created file example <file>-2.log is the latest appended log

# File lib/stackify_apm/logger/log_device.rb, line 32
def shift_log_age
  # set a temporary filename that doesn't have the increment prefix and .log format.
  temp_filename = @filename.to_s.sub(/\-(d*\.?\d*).log/, '')
  # set a temporary file increment while stripping the current filename and retain the number
  txttemp_fileincrement = @filename.to_s.sub(temp_filename, '').delete('^0-9')
  # convert the string value to integer
  current_fileprefix = txttemp_fileincrement.to_i
  # assign as filename counter
  filename_counter = current_fileprefix
  if FileTest.exist?(@filename)
    filename_counter = current_fileprefix + 1
  else
    ctr_flagger = 0
    # a loop that check if the number of filenames if exists or not
    1.upto(current_fileprefix) do |i|
      temp_oldfile = "#{temp_filename}-#{i}.log"
      ctr_flagger += 1 if FileTest.exist?(temp_oldfile)
    end
    # if the counter is 0 then set the filename counter to 1
    filename_counter = 1 if ctr_flagger < 0
  end

  @dev.close rescue nil

  temp_newfilename = "#{temp_filename}-#{filename_counter}.log"
  @filename = temp_newfilename
  @dev = create_logfile(@filename)

  true
end
shift_log_period(period_end) click to toggle source

This is the monkeypatch of core Logger method where reformats the file name when creating the file log.

# File lib/stackify_apm/logger/log_device.rb, line 64
def shift_log_period(period_end)
  suffix = period_end.strftime(@shift_period_suffix)
  age_file = "#{@filename}.#{suffix}"
  if FileTest.exist?(age_file)
    # try to avoid filename crash caused by Timestamp change.
    idx = 1
    # .99 can be overridden; avoid too much file search with 'loop do'
    while idx < 100
      idx += 1
      age_file = "#{@filename}-#{idx}.#{suffix}"
      break unless FileTest.exist?(age_file)
    end
  end
  @dev.close rescue nil
  File.rename(@filename.to_s, age_file)
  @dev = create_logfile(@filename)

  true
end
write(message) click to toggle source
# File lib/stackify_apm/logger/log_device.rb, line 13
def write(message)
  if @filename
    if FileTest.exist?(@filename)
    else
      @dev = create_logfile(@filename)
    end
  end
  write_without_apm(message)
end
Also aliased as: write_without_apm
write_without_apm(message)
Alias for: write