新建了一个EB环境,来跑一个staging模式的rails。EB环境变量设置好后,部署成功。一系列测试都没问题,结果却在一个不起眼的地方出了点幺蛾子。

Rails代码中,没有特殊配置config.logger,因此staging模式时,默认的输出日志为logs/staging.log, 但是在EB的Web console中尝试获取Full Logs后,在BundleLogs中发现没有staging.log。

赶紧调查试验了一下,并将调查结果记录如下。

调查EB logs日志

  1. 登陆EB中的EC2 Instance,试图寻找到为何staging.log没有在BundleLogs中。
  2. 切换到BundleLogs打包下载的源目录/var/app/support/logs/, 发现只有development.log和production.log的软连接, 而没有staging.log

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [ec2-user@ip-172-31-14-22 logs]$ ll
    总用量 8
    -rw-r--r-- 1 root root 0 12月 20 10:01 access.log
    lrwxrwxrwx 1 root root 36 12月 20 06:21 development.log -> /var/app/current/log/development.log
    drwxr-xr-x 2 webapp webapp 4096 12月 20 10:01 healthd
    -rw-r--r-- 1 webapp webapp 0 12月 20 10:01 passenger.log
    lrwxrwxrwx 1 root root 35 12月 20 06:21 production.log -> /var/app/current/log/production.log
    drwxr-xr-x 2 root root 4096 12月 20 10:01 rotated
    [ec2-user@ip-172-31-14-22 logs]$
  3. 在/opt/elasticbeanstalk/目录下遍寻生成development.log和production.log的脚本。最终寻得是在/opt/elasticbeanstalk/hooks/preinit/24_rails_support.sh中设置了development.log和production.log的软连接。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    [ec2-user@ip-172-31-14-22 elasticbeanstalk]$ cat hooks/preinit/24_rails_support.sh
    #!/usr/bin/env bash
    #==============================================================================
    # 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 -xe
    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)
    EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
    # For builtin Rails logging support
    ln -sf $EB_APP_DEPLOY_DIR/log/production.log $EB_APP_LOG_DIR/production.log
    ln -sf $EB_APP_DEPLOY_DIR/log/development.log $EB_APP_LOG_DIR/development.log
    [ec2-user@ip-172-31-14-22 elasticbeanstalk]$
  4. 既然EB默认没有建立staging.log的软连接,那就自己动手建一个。在代码根目录的.ebextensions下添加名为10-add-staging-log-link.config的文件,写入如下内容

    1
    2
    3
    4
    commands:
    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'
  5. 重新发布代码到EB, 发布完成后,可以看到在目录目录/var/app/support/logs/下,staging.log已经建立了对应的软连接了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [ec2-user@ip-172-31-47-16 logs]$ ll
    总用量 16
    -rw-r--r-- 1 root root 150 12月 21 06:32 access.log
    lrwxrwxrwx 1 root root 36 12月 21 06:16 development.log -> /var/app/current/log/development.log
    drwxr-xr-x 2 webapp webapp 4096 12月 21 06:32 healthd
    -rw-r--r-- 1 webapp webapp 3499 12月 21 06:32 passenger.log
    lrwxrwxrwx 1 root root 35 12月 21 06:16 production.log -> /var/app/current/log/production.log
    drwxr-xr-x 2 root root 4096 12月 21 06:16 rotated
    lrwxrwxrwx 1 root root 32 12月 21 06:17 staging.log -> /var/app/current/log/staging.log
    [ec2-user@ip-172-31-47-16 logs]$
  6. 在EB的Web Console中点击Full Logs下载BundleLogs,解压后可以看到/var/app/support/logs/下有staging.log了,rotated目录下也有rotate的staging的log

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    [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.log
    6 directories, 28 files
    [carlshen@carl var]$

留言