Class: Rails::Pretty::Logger::RailsLogger::LoggerDevice

Inherits:
LogDevice
  • Object
show all
Includes:
Period
Defined in:
lib/rails/pretty/logger/rails_logger.rb

Constant Summary

Constants included from Period

Period::SiD

Instance Method Summary collapse

Methods included from Period

next_rotate_time, previous_period_end

Constructor Details

#initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, file_count: nil) ⇒ LoggerDevice

Returns a new instance of LoggerDevice.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails/pretty/logger/rails_logger.rb', line 81

def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, file_count: nil)
  @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = @file_count = nil
  mon_initialize
  set_dev(log)
  if @filename
    @shift_age = shift_age || 7
    @shift_size = shift_size || 1048576
    @shift_period_suffix = shift_period_suffix || '%Y%m%d'
    @file_count = file_count || 48
    unless @shift_age.is_a?(Integer)
      base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
      @next_rotate_time = next_rotate_time(base_time, @shift_age)
    end
  end
end

Instance Method Details

#delete_old_file(file_path) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/rails/pretty/logger/rails_logger.rb', line 142

def delete_old_file(file_path)
  day_dir = File.dirname(file_path)
  month_dir = File.expand_path("..",day_dir)
  year_dir = File.expand_path("../..",day_dir)
  File.delete(file_path) if File.exist?(file_path)
  Dir.rmdir(day_dir) if Dir.empty?(day_dir)
  Dir.rmdir(month_dir) if month_dir.empty?
  Dir.rmdir(year_dir) if year_dir.empty?
end

#shift_log_period(period_end) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rails/pretty/logger/rails_logger.rb', line 97

def shift_log_period(period_end)
  suffix = period_end.strftime(@shift_period_suffix)

  suffix_year = period_end.strftime('%Y')
  suffix_month = period_end.strftime('%m')
  suffix_day = period_end.strftime('%d')

  if @shift_age == 'hourly'
    suffix = period_end.strftime('%Y%m%d_%H%M')
  end

  age_file = "#{@filename}.#{suffix}"

  if FileTest.exist?(age_file)
    # try to avoid filename crash caused by Timestamp change.
    idx = 0
    # .99 can be overridden; avoid too much file search with 'loop do'
    while idx < 100
      idx += 1
      age_file = "#{@filename}.#{suffix}.#{idx}"
      break unless FileTest.exist?(age_file)
    end
  end

  #delete old files
  log_files = Dir[ File.join(Rails.root, 'log', 'hourly') + "/#{suffix_year}/**/*"].reject {|fn| File.directory?(fn) }
  while (log_files.length > @file_count) do
    arr = log_files.reduce([]){|memo, log_file| memo <<  File.ctime(log_file).to_i}
    file_index = arr.index(arr.min)
    file_path = log_files[file_index]
    delete_old_file(file_path)
    log_files = Dir[ File.join(Rails.root, 'log', 'hourly') + "/#{suffix_year}/**/*"].reject {|fn| File.directory?(fn) }
  end

  @dev.close rescue nil

  File.rename("#{@filename}", age_file)
  old_log_path = Rails.root.join(age_file)
  new_path = File.join(Rails.root, 'log', 'hourly', suffix_year, suffix_month, suffix_day)
  FileUtils.mkdir_p new_path
  FileUtils.mv old_log_path, new_path, :force => true
  @dev = create_logfile(@filename)
  return true
end