0
0

More than 3 years have passed since last update.

Laravel5.6以降のLog設定の小ネタ

Posted at

config/log.phpでログを設定するようになってからの設定方法についての小ネタ。
ドキュメントに明示されておらず、StackOverflow等でもなかなか出てこないので書いておく。

monolog driver以外でも、Formatterはconfig/log.phpで指定できる

Monolog Formatters
When using the monolog driver, the Monolog LineFormatter will be used as the default formatter. However, you may customize the type of formatter passed to the handler using the formatter and formatter_with configuration options:

'browser' => [
    'driver' => 'monolog', // <= simpleドライバなどでもformatter指定できる
    'handler' => Monolog\Handler\BrowserConsoleHandler::class,
    'formatter' => Monolog\Formatter\HtmlFormatter::class,
    'formatter_with' => [
        'dateFormat' => 'Y-m-d',
    ],
],
  • ドキュメントには monolog ドライバー用のオプションのように書かれているが、 simpledaily のdriverであっても同様に、formatterパラメータが有効
    • 同様にformatter_withも有効

tap()時、getHandlers()で全ハンドラを回さなくても直接$logger->pushProcessor()できる

ドキュメントの tap() のサンプルが以下のように書かれているため、プロセッサの設定でも同様のループを書いていることが多い

// tapの処理サンプル
public function __invoke($logger)
{
    foreach ($logger->getHandlers() as $handler) {
        $handler->setFormatter(...);
    }
}
public function __invoke($logger)
{
    foreach ($logger->getHandlers() as $handler) {
        // ならこうやろ!
        $handler->pushProcessor(...);
    }
}

※このサンプルに引っ張られて上記Formatterの設定を tap() でやってしまう疑惑

  • __invoke($logger) に渡される実体は Monolog\Logger
  • Loggerのスタック全体に対して適用するProcessorを pushProcessor() で突っ込める ⇒ソース

            public function __invoke($logger)
            {
                // 全部に適用したいならこれで十分
                $logger->pushProcessor(...);
            }
    
0
0
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
0
0