Class: StackifyRubyAPM::LogDevice Private

Inherits:
Logger::LogDevice
  • Object
show all
Defined in:
lib/stackify_apm/logger/log_device.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

MAX_LOG_FILES_COUNT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

10

Instance Method Summary collapse

Instance Method Details

#add_log_header(file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/stackify_apm/logger/log_device.rb', line 23

def add_log_header(file)
  # Make it only empty.
end

#create_logfile(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stackify_apm/logger/log_device.rb', line 85

def create_logfile(filename)
  logdev = super

  begin
    dir_name = File.dirname(filename)
    StackifyRubyAPM::Util.checkOrCreateFolder(dir_name)
    File.chmod(0o777, 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_ageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
# 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

#write_without_apmObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
# File 'lib/stackify_apm/logger/log_device.rb', line 12

alias_method 'write_without_apm', 'write'