LoginSignup
3
1

More than 1 year has passed since last update.

Fargateでphp-fpmのログを出力する

Last updated at Posted at 2021-05-22

はまったので。下記が必要。

php-fpmの設定

[global]
error_log = /proc/self/fd/2
log_limit = 8192
daemonize = no

[www]
access.log = /proc/self/fd/2

clear_env = no

catch_workers_output = yes
decorate_workers_output = no
  • error_logをstdoutにリダイレクト
  • daemonizeをnoにしておかないとフォアグラウンドプロセスにならない。
  • access_logもstdoutにリダイレクト
  • catch_workers_outputをyesにすると php://stdout と php://stderr をerror_logに出力する。
  • decorate_workers_outputをnoにすると余計なprefixを削除してくれる。
  • clear_envをnoにしないとIAMRoleを使ったS3へのアップロードに失敗する。(clear_envがyesの場合は認証に必要なAWS_CONTAINER_CREDENTIALS_RELATIVE_URIという環境変数を消してくれる。)

Laravelの設定

下記でstdout, stderrにログが送られるのであとは良きようにCloudWatchLogsかfirelensで好きなところにlogがたまる。
エラーはslackにリアルタイムに送られる

<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Log Channel
    |--------------------------------------------------------------------------
    |
    | This option defines the default log channel that gets used when writing
    | messages to the logs. The name specified in this option should match
    | one of the channels defined in the "channels" configuration array.
    |
    */

    'default' => env('LOG_CHANNEL', 'docker'),

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily', 'stdout'],
            'ignore_exceptions' => false,
        ],

        'docker' => [
            'driver' => 'stack',
            'channels' => ['stderr', 'stdout', 'slack'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'error',
        ],

        'papertrail' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => SyslogUdpHandler::class,
            'handler_with' => [
                'host' => env('PAPERTRAIL_URL'),
                'port' => env('PAPERTRAIL_PORT'),
            ],
        ],

        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stderr',
                'level' => 'error'
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => 'debug',
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => 'debug',
        ],

        'null' => [
            'driver' => 'monolog',
            'handler' => NullHandler::class,
        ],
        'stdout' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stdout',
                'level' => 'debug'
            ],
        ],
    ],

];
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1