class Rails::Pretty::Logger::RailsLogger::LoggerDevice
Public Class Methods
new(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, file_count: nil)
click to toggle source
# 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
Public Instance Methods
delete_old_file(file_path)
click to toggle source
# 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)
click to toggle source
# 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