Clear development log while start server 🗑

Do you find yourself manually clearing your Rails development logs often? or forgot to do so and it results in having file-size is greater than 1000 MB 😕

Here's a simple solution to automate the process!

Create an initializer file under config/initializers and add the following code:

# config/initializers/clear_development_log.rb

if Rails.env.development?
  logfile = File.join(Rails.root, 'log', 'development.log')
  if File.size?(logfile).to_i > 50.megabytes"Clearing development log...")
    File.truncate(logfile, 0)

This code will automatically clear your development logs on startup, as long as the log file size exceeds 50 MB.

If you wanted to clear development log every-time server starts, you can use following code -

# config/initializers/clear_development_log.rb

`rake log:clear` if Rails.env.development?