Laravel Monolog LineFormatter 日期时间模式

问题描述 投票:0回答:2

我正在尝试在 laravel 中创建自定义日志,并且我正在使用 monlog\LineFormatter 类来格式化我的最终日志

'events' => [
            'driver' => 'daily',
            'formatter' =>LineFormatter::class,
            'formatter_with' => [
                'format' => "[%datetime%] %channel%.%level_name%: %context%\n",
            ],
            'path' => storage_path('logs/events.log'),
            'level' => 'info'
        ] 

不,我在日志文件中收到如下日志

[2022-01-27T08:30:33.627980+00:00] local.INFO: {"request-id":"5f9c3819-97b3-4439-87ab-30c58bffd2a5","event_name":"cancel_pending_withdraw","message":"action webhook sent"}

我希望日期时间格式是这样的

[2022-01-27T08:30:33.627980+00:00]  ==> [2022-01-27 08:30:33]

我如何通过 monolog Lineformatter 更改此格式化程序。

laravel monolog
2个回答
0
投票

所以我得到了解决方案,最简单的方法如下

'events' => [
            'driver' => 'daily',
            'formatter' =>LineFormatter::class,
            'formatter_with' => [
                'format' => "[%datetime%] %channel%.%level_name%: %context%\n",
                'dateFormat' => "Y-m-d H:i:s"
            ],
            'path' => storage_path('logs/unique-events.log'),
            'level' => 'info'
        ]

0
投票

Laravel 10

第二个参数或 LineFormatter 是日期格式,如果您检查其源代码,如下所示

public function __construct(?string $format = null, ?string $dateFormat = null, bool $allowInlineLineBreaks = false, bool $ignoreEmptyContextAndExtra = false, bool $includeStacktraces = false)
    {
        $this->format = $format === null ? static::SIMPLE_FORMAT : $format;
        $this->allowInlineLineBreaks = $allowInlineLineBreaks;
        $this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra;
        $this->includeStacktraces($includeStacktraces);
        parent::__construct($dateFormat);
    }

如何在第二个参数上传递日期格式

   <?php
    
    namespace App\Logging;
    
    use Illuminate\Log\Logger;
    use Monolog\Formatter\LineFormatter;
    use Illuminate\Http\Request;
    
    class CustomizeFormatter
    {
    
    
        /**
         * The current HTTP request instance.
         *
         * @var \Illuminate\Http\Request
         */
        protected $request;
    
        /**
         * Create a new instance.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return void
         */
        public function __construct(Request $request)
        {
            $this->request = $request;
        }
    
    
        /**
         * Customize the given logger instance.
         */
        public function __invoke(Logger $logger): void
        {
            foreach ($logger->getHandlers() as $handler) {
                $handler->setFormatter(new LineFormatter(
                    "[%datetime%] [". $this->request->ip()."] %channel%.%level_name%: %message% %context% %extra.ip% \n",
                    'Y-m-d H:i:s'
                ));
            }
        }
    
    }

在 config/logging.php 上

   'channels' => [
  

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
            'replace_placeholders' => true,
            'permission' => 0777,
            'tap' => [App\Logging\CustomizeFormatter::class]
        ],
        
    ]
© www.soinside.com 2019 - 2024. All rights reserved.