console 로그와 웹 로그 분리하기

Submitted by river - 등록 7 years ago - 수정 7 years ago

로그 설정을 daily로 하고, artisan command 등의 console 명령을 스케쥴링을 통하여 수행하는 경우 console 계정과 웹 계정의 차이로 인해 다음의 문제가 발생하는 경우가 있다.

Failed to open stream: Permission denied

원인은 스택오버플로우 Laravel daily log created with wrong permissions를 참고하면 될 것 같고, 이를 해결하기 위해 로그파일을 분리하는 방법을 정리해 봤다.

bootstrap\app.php

/*
|--------------------------------------------------------------------------
| Configure Monolog
|--------------------------------------------------------------------------
|
| console 유저와 웹 유저가 달라서 log 파일 접근시 권한 문제 발생해서 로그 파일 분리
| console에서 동작시 -artisan- 접두어가 붙은 로그 파일 사용
|
*/

$app->configureMonologUsing(function ($monolog) use ($app) {
    $filePrefix = $app->runningInConsole() ? '-artisan' : '';

    // debug 로그 파일 별도로 분리. 개발시 편리
    if (config('app.debug', false)) {
        $filename = storage_path('logs/laravel' . $filePrefix . '-debug.log');
        $handler = new Monolog\Handler\RotatingFileHandler($filename, config('app.log_max_files', 5), Monolog\Logger::DEBUG);
        $handler->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true));
        $monolog->pushHandler($handler);
    }

    $filename = storage_path('logs/laravel' . $filePrefix . '.log');
    $handler = new Monolog\Handler\RotatingFileHandler($filename, config('app.log_max_files', 5), Monolog\Logger::INFO);
    $handler->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true));
    $monolog->pushHandler($handler);
});

관련글

comments powered by Disqus