我们正在运行弹性beanstalk(64位Amazon Linux 2016.09 v2.3.1运行Ruby 2.3(PUMA))使用Rails App。
应用程序日志正在写入/var/apps/current/log/production.rb
标准。作为用EB的标准配置,该文件链接到/var/apps/containerfiles/logs/
并用于旋转并上传到S3。
出于某种原因,每次我们eb deploy
时,production.log
似乎都被覆盖或截断,这似乎是意想不到的。
我们是否对某些内容进行了错误配置,您如何建议我们调试?
我们得出了(也许是显而易见的)结论,即EB部署没有日志魔法。它只需替换/var/apps/current/
目录,包括/var/apps/current/log
。从而删除所有现有日志。
因此,我们的解决方案是将日志放在单独的文件夹中,然后将日志放在patch eb中,以了解放置日志的位置。通过在app_log_dir
(/var/app/containerfiles/logs/
)中覆盖production.log
符号链接,我们仍然依靠EB的正常过程旋转和发布到S3。
.ebextensions/log-rotation.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/01a_override_log_symlinks.sh":
mode: "000777"
content: |
#!/bin/bash
EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)
CUSTOM_APPLOG_DIR=/var/log/applog
mkdir -p $CUSTOM_APPLOG_DIR
chown webapp $CUSTOM_APPLOG_DIR
chmod 777 $CUSTOM_APPLOG_DIR
cd $EB_APP_LOG_DIR
ln -sf $CUSTOM_APPLOG_DIR/production.log production.log
ln -sf $CUSTOM_APPLOG_DIR/development.log development.log
/config/environments/production.rb
...
# Specific for Rails 5!
config.paths['log'] = "/var/log/applog/#{Rails.env}.log"
...
我也感到非常惊讶,当我发现这一点时,这似乎是我们想要的。日志文件的所有者应为/var/app/containerfiles。
在Amazon Linux 2上,我刚刚添加了一个Deploy钩子来重新切换它们,并且似乎效果很好。。。
这是.platform/hooks/postdeploy/logs.sh
的内容:
#!/bin/bash
# Switch over the master location of the log files to be /var/app/containerfiles/logs/, with a symlink into /var/app/current/log/ so logs are kept between deploys
# Effectively reversing this line:
# [INFO] adding builtin Rails logging support
# [INFO] log publish feature is enabled, setup configurations for rails
# [INFO] create soft link from /var/app/current/log/production.log to /var/app/containerfiles/logs/production.log
if [ -L /var/app/containerfiles/logs/production.log ]; then
unlink /var/app/containerfiles/logs/production.log
mv /var/app/current/log/production.log /var/app/containerfiles/logs/production.log
fi
touch /var/app/containerfiles/logs/production.log
ln -sf /var/app/containerfiles/logs/production.log /var/app/current/log/production.log