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.



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

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

#available_log_path(path) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/rails/pretty/logger/rails_logger.rb', line 135

def available_log_path(path)
  candidate = path
  index = 0
  while File.exist?(candidate)
    index += 1
    candidate = "#{path}.#{index}"
  end
  candidate
end

#delete_old_file(file_path) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/rails/pretty/logger/rails_logger.rb', line 163

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.exist?(day_dir) && Dir.empty?(day_dir)
  Dir.rmdir(month_dir) if Dir.exist?(month_dir) && Dir.empty?(month_dir)
  Dir.rmdir(year_dir) if Dir.exist?(year_dir) && Dir.empty?(year_dir)
end

#delete_old_hourly_filesObject



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

def delete_old_hourly_files
  log_files = hourly_log_files
  while log_files.length > @file_count
    delete_old_file(log_files.min_by { |log_file| hourly_log_sort_key(log_file) })
    log_files = hourly_log_files
  end
end

#hourly_log_filesObject



153
154
155
156
157
# File 'lib/rails/pretty/logger/rails_logger.rb', line 153

def hourly_log_files
  log_prefix = "#{File.basename(@filename)}."
  Dir[File.join(Rails.root, 'log', 'hourly', '**', '*')]
    .select { |file| File.file?(file) && File.basename(file).start_with?(log_prefix) }
end

#hourly_log_sort_key(file) ⇒ Object



159
160
161
# File 'lib/rails/pretty/logger/rails_logger.rb', line 159

def hourly_log_sort_key(file)
  File.basename(file)[/\.([0-9]{8}_[0-9]{4})(?:\.[0-9]+)?\z/, 1] || File.mtime(file).utc.strftime("%Y%m%d_%H%M")
end

#rotation_lock_pathObject



131
132
133
# File 'lib/rails/pretty/logger/rails_logger.rb', line 131

def rotation_lock_path
  Rails.root.join("tmp", "rails_pretty_logger", "#{File.basename(@filename)}.rotate.lock").to_s
end

#shift_log_period(period_end) ⇒ Object



96
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
# File 'lib/rails/pretty/logger/rails_logger.rb', line 96

def shift_log_period(period_end)
  with_rotation_lock do
    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 = available_log_path("#{@filename}.#{suffix}")

    @dev.close rescue nil

    File.rename("#{@filename}", age_file)
    new_path = File.join(Rails.root, 'log', 'hourly', suffix_year, suffix_month, suffix_day)
    FileUtils.mkdir_p new_path
    destination = available_log_path(File.join(new_path, File.basename(age_file)))
    FileUtils.mv age_file, destination
    delete_old_hourly_files
    @dev = create_logfile(@filename)
    true
  end
end

#with_rotation_lockObject



123
124
125
126
127
128
129
# File 'lib/rails/pretty/logger/rails_logger.rb', line 123

def with_rotation_lock
  FileUtils.mkdir_p(File.dirname(rotation_lock_path))
  File.open(rotation_lock_path, File::RDWR | File::CREAT, 0644) do |lock|
    lock.flock(File::LOCK_EX)
    yield
  end
end