how to delete rails log file after certain size

前端 未结 5 1145
礼貌的吻别
礼貌的吻别 2020-12-28 19:15

I have a daemon that runs constantly which fills up the log file(development.log or production.log) pretty quickly. What is the best way to delete the log file after certain

5条回答
  •  被撕碎了的回忆
    2020-12-28 20:18

    Or even better, if all your environments are on either Mac or Linux, and have /usr/sbin/rotatelogs, just use that. It's much more flexible, and doesn't have the data loss issue that logrotate has (even if you use copytruncate).

    Add this inside config/application.rb (or just config/environments/production.rb if you only want rotation in prod):

    log_pipe = IO.popen("/usr/sbin/rotatelogs #{Rails.root}/log/#{Rails.env}.%Y%m%d.log 86400", 'a')
    config.logger = Logger.new(log_pipe)
    

    (From this blog post)

提交回复
热议问题