Lumen에서 daily log 사용하기

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

Lumen에서는 storage/logs/lumen.log 하나의 파일에 로그가 기록된다. 로그에 대한 설정은 getMonologHandler에서 행해진다.

Laravel\Lumen\Application

...
    /**
     * Register container bindings for the application.
     *
     * @return void
     */
    protected function registerLogBindings()
    {
        $this->singleton('Psr\Log\LoggerInterface', function () {
            return new Logger('lumen', [$this->getMonologHandler()]);
        });
    }

    /**
     * Get the Monolog handler for the application.
     *
     * @return \Monolog\Handler\AbstractHandler
     */
    protected function getMonologHandler()
    {
        return (new StreamHandler(storage_path('logs/lumen.log'), Logger::DEBUG))
                            ->setFormatter(new LineFormatter(null, null, true, true));
    }
...

날짜별로 로그를 기록하려면 bootstrap/app.php를 다음과 같이 수정하면 된다.

bootstrap/app/php

<?php

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\WebProcessor;

...

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Configure logger
|--------------------------------------------------------------------------
*/

// Replace default log handler with daily log handler
$logger = $app->make('Psr\Log\LoggerInterface');
$logger->popHandler();
$logger->pushHandler(
    (new RotatingFileHandler(storage_path('logs/lumen.log'), Logger::DEBUG))
        ->setFormatter(new LineFormatter(null, null, true, true))
);

// processor, adding URI, IP address etc. to the log
$logger->pushProcessor(new WebProcessor);

// processor, memory usage
$logger->pushProcessor(new MemoryUsageProcessor);

...

$logger->popHandler()로 기본 로그처리기를 삭제하고, $logger->pushHander()를 이용해서 RotatingFileHandler를 등록한다.

comments powered by Disqus