如何旋转我的Rails 5日志?

Dav*_*ave 5 linux logging ruby-on-rails logrotate ruby-on-rails-5

我在Ubuntu 14.04上运行Rails 5.有没有办法在不依赖Linux logrotate系统的情况下轮换我的日志?我有这个设置......

myuser@myapp:~$ cat /etc/logrotate.d/myapp
/home/rails/myapp/log/*.log {
  daily
  missingok
  rotate 2
  compress
  delaycompress
  notifempty
  copytruncate
}
Run Code Online (Sandbox Code Playgroud)

但我的日志永远不会旋转.看他们有多臃肿......

myuser@myapp:~$ ls -al /home/rails/myapp/log/
total 3958356
drwxr-xr-x  2 rails rails       4096 Jul  3 22:31 .
drwxr-xr-x 15 rails rails       4096 Sep 21 17:21 ..
-rw-rw-r--  1 rails rails          0 Jun 22 10:22 development.log
-rw-rw-r--  1 rails rails      14960 Jun  1 22:39 development.log.1
-rw-rw-r--  1 rails rails          0 Oct 22  2016 .keep
-rw-r--r--  1 rails rails  198362787 Oct 31 16:28 production.log
-rw-r--r--  1 rails rails    8615654 Jul  3 22:31 production.log.1
-rw-r--r--  1 rails rails  640621243 Jun 29 13:16 production.log.2.gz
-rw-rw-r--  1 rails rails 2856792698 Oct 31 17:12 sidekiq.log
-rw-rw-r--  1 rails rails  348853619 Jul  3 22:31 sidekiq.log.1
-rw-rw-r--  1 rails rails          0 Jul  3 22:31 test.log
-rw-rw-r--  1 rails rails      54246 Jul  3 22:31 test.log.1
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以让日志轮换,或者有办法修复我已包含的配置吗?

编辑:这是设置的cron脚本

myuser@myapp:~$ cat /etc/cron.daily/logrotate
#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

编辑:根据评论我尝试将其添加到我的config/environment/production.rb文件中...

config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024)
Run Code Online (Sandbox Code Playgroud)

但是日志变得越来越大而没有旋转.

nba*_*ari 8

通过遵循12因子方法(将日志视为事件流),您可以"应该"将此任务委派给主管.

例如,通过使用immortal,它将为您完成所有旋转过程,而不依赖于操作系统.

基本配置文件(run.yml)可能如下所示:

cmd: bundle exec unicorn -c unicorn.rb
cwd: /arena/app-1
env:
    DEBUG: 1
    ENVIRONMENT: production
log:
    file: /var/log/app-1.log
    age: 86400 # seconds
    num: 7     # int
    size: 1    # MegaBytes
    timestamp: true # will add timesamp to log
Run Code Online (Sandbox Code Playgroud)

如果您想分割日志stderr&stdout,可以使用:

cmd: bundle exec unicorn -c unicorn.rb
cwd: /arena/app-1
env:
    DEBUG: 1
    ENVIRONMENT: production
log:
  file: /var/log/app.log
  age: 86400 # seconds
  num: 7     # int
  size: 1    # MegaBytes
stderr:
  file: /var/log/app-error.log
  age: 86400 # seconds
  num: 7     # int
  size: 1    # MegaBytes
  timestamp: true # will add timesamp to log
Run Code Online (Sandbox Code Playgroud)

作为旁注,来自12因素的网站:

十二因素应用程序进程永远不应该守护或写入PID文件.相反,依靠操作系统的进程管理器来管理输出流,响应崩溃的进程,并处理用户启动的重新启动和关闭.

[免责声明:我是不朽的作者],背后的一个想法确实是大规模地覆盖应用程序的自动化,而不需要担心在没有正确旋转的日志之外填充磁盘除了可用于部署(启动)/restart)只需修改一个run.yml文件而不需要root权限就能以最简单的方式完成.