如何在Elastic Beanstalk的Rails环境中配置staging.log为可下载
新建了一个EB环境,来跑一个staging模式的rails。EB环境变量设置好后,部署成功。一系列测试都没问题,结果却在一个不起眼的地方出了点幺蛾子。
Rails代码中,没有特殊配置config.logger,因此staging模式时,默认的输出日志为logs/staging.log, 但是在EB的Web console中尝试获取Full Logs后,在BundleLogs中发现没有staging.log。
赶紧调查试验了一下,并将调查结果记录如下。
调查EB logs日志
- 登陆EB中的EC2 Instance,试图寻找到为何staging.log没有在BundleLogs中。
切换到BundleLogs打包下载的源目录/var/app/support/logs/, 发现只有development.log和production.log的软连接, 而没有staging.log
123456789[ec2-user@ip-172-31-14-22 logs]$ ll总用量 8-rw-r--r-- 1 root root 0 12月 20 10:01 access.loglrwxrwxrwx 1 root root 36 12月 20 06:21 development.log -> /var/app/current/log/development.logdrwxr-xr-x 2 webapp webapp 4096 12月 20 10:01 healthd-rw-r--r-- 1 webapp webapp 0 12月 20 10:01 passenger.loglrwxrwxrwx 1 root root 35 12月 20 06:21 production.log -> /var/app/current/log/production.logdrwxr-xr-x 2 root root 4096 12月 20 10:01 rotated[ec2-user@ip-172-31-14-22 logs]$在/opt/elasticbeanstalk/目录下遍寻生成development.log和production.log的脚本。最终寻得是在/opt/elasticbeanstalk/hooks/preinit/24_rails_support.sh中设置了development.log和production.log的软连接。
123456789101112131415161718192021222324252627[ec2-user@ip-172-31-14-22 elasticbeanstalk]$ cat hooks/preinit/24_rails_support.sh#==============================================================================# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.## Licensed under the Amazon Software License (the "License"). You may not use# this file except in compliance with the License. A copy of the License is# located at## https://aws.amazon.com/asl/## or in the "license" file accompanying this file. This file is distributed on# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or# implied. See the License for the specific language governing permissions# and limitations under the License.#==============================================================================set -xeEB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)# For builtin Rails logging supportln -sf $EB_APP_DEPLOY_DIR/log/production.log $EB_APP_LOG_DIR/production.logln -sf $EB_APP_DEPLOY_DIR/log/development.log $EB_APP_LOG_DIR/development.log[ec2-user@ip-172-31-14-22 elasticbeanstalk]$既然EB默认没有建立staging.log的软连接,那就自己动手建一个。在代码根目录的.ebextensions下添加名为10-add-staging-log-link.config的文件,写入如下内容
1234commands:01_link_staging_log:command: 'EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir); EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir); sudo ln -sf $EB_APP_DEPLOY_DIR/log/staging.log $EB_APP_LOG_DIR/staging.log'重新发布代码到EB, 发布完成后,可以看到在目录目录/var/app/support/logs/下,staging.log已经建立了对应的软连接了。
12345678910[ec2-user@ip-172-31-47-16 logs]$ ll总用量 16-rw-r--r-- 1 root root 150 12月 21 06:32 access.loglrwxrwxrwx 1 root root 36 12月 21 06:16 development.log -> /var/app/current/log/development.logdrwxr-xr-x 2 webapp webapp 4096 12月 21 06:32 healthd-rw-r--r-- 1 webapp webapp 3499 12月 21 06:32 passenger.loglrwxrwxrwx 1 root root 35 12月 21 06:16 production.log -> /var/app/current/log/production.logdrwxr-xr-x 2 root root 4096 12月 21 06:16 rotatedlrwxrwxrwx 1 root root 32 12月 21 06:17 staging.log -> /var/app/current/log/staging.log[ec2-user@ip-172-31-47-16 logs]$在EB的Web Console中点击Full Logs下载BundleLogs,解压后可以看到/var/app/support/logs/下有staging.log了,rotated目录下也有rotate的staging的log
123456789101112131415161718192021222324252627282930313233343536373839[carlshen@carl var]$ tree.├── app│ └── support│ └── logs│ ├── access.log│ ├── passenger.log│ ├── rotated│ │ ├── access.log1513753261.gz│ │ ├── access.log1513756861.gz│ │ ├── access.log1513760461.gz│ │ ├── access.log1513764061.gz│ │ ├── passenger.log1513753261.gz│ │ ├── passenger.log1513764061.gz│ │ ├── staging.log1513753261.gz│ │ ├── staging.log1513756861.gz│ │ ├── staging.log1513760461.gz│ │ └── staging.log1513764061.gz│ └── staging.log└── log├── cfn-hup.log├── cfn-init-cmd.log├── cfn-init.log├── cloud-init-output.log├── cloud-init.log├── cron├── eb-activity.log├── eb-cfn-init-call.log├── eb-cfn-init.log├── eb-commandprocessor.log├── eb-publish-logs.log├── eb-tools.log├── healthd│ └── daemon.log├── messages└── yum.log6 directories, 28 files[carlshen@carl var]$