【问题标题】:Extend Laravel log prefix to include custom environment variable扩展 Laravel 日志前缀以包含自定义环境变量
【发布时间】:2020-08-29 17:41:51
【问题描述】:

对于 Laravel 5.7 应用,我想在默认日志前缀中添加一些额外的信息。

当前的日志条目如下所示:

[2020-05-13 13:07:42] production.INFO: Information here...

我想扩展前缀来实现:

[2020-05-13 13:07:42] PREFIX.production.INFO: Information here...

PREFIX 来自环境变量,LOG_PREFIX

LOG_CHANNEL=daily
LOG_PREFIX=PREFIX

我不介意在自定义格式化程序中使用正则表达式将 production.INFO 更改为 PREFIX.production.INFO。

我在日志频道中添加了一个水龙头:

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 7,
    'tap' => [App\Logging\CustomizeFormatter::class],
]

我在 CustomizeFormatter 类的调用方法中遇到问题:

class CustomizeFormatter
{
    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($logger)
    {
        // What to add here?
    }
}

那里可以用什么来做我需要的事情?

这是走错路了吗?

谢谢。

使用在此处找到的答案https://stackoverflow.com/a/34289039/121946 我可以使用以下命令在消息末尾添加前缀作为额外信息:

public function __invoke($logger)
{
    foreach ($logger->getHandlers() as $handler) {
      $handler->pushProcessor(function ($record) {
          $record['extra']['prefix'] = env('LOG_PREFIX');
          return $record;
      });
  }
}

查看$record,我没有看到完整的日志文本,只有自定义部分:

array:7 [▼
  "message" => "Information here..."
  "context" => []
  "level" => 200
  "level_name" => "INFO"
  "channel" => "production"
  "datetime" => DateTime @1589377784 {#953 ▶}
  "extra" => []
]

【问题讨论】:

    标签: laravel laravel-5.7 monolog


    【解决方案1】:

    将其添加到附加项中不会显示您想要的方式,这是默认的行格式:

    [%datetime%] %channel%.%level_name%: %message% %context% %extra%
    

    正如你所看到的那样,额外的东西在最后,所以它会产生这样的东西:

    [2020-05-13 16:50:20] development.INFO: [] {"prefix":"MyPrefix"}
    

    你可以这样做:

    <?php
    
    namespace App\Logging;
    
    use Illuminate\Log\Logger;
    use Illuminate\Support\Arr;
    use Monolog\Formatter\LineFormatter;
    
    class CustomizeFormatter
    {
        /**
         * Customize the given logger instance.
         *
         * @param  Logger  $logger
         *
         * @return void
         */
        public function __invoke($logger)
        {
            foreach ($logger->getHandlers() as $handler) {
                $handler->pushProcessor(function ($record) {
                    return Arr::add($record, 'prefix', env('LOG_PREFIX'));
                });
                $handler->setFormatter(tap(new LineFormatter(
                    "[%datetime%] %prefix%.%channel%.%level_name%: %message% %context% %extra%\n",
                    'Y-m-d H:i:s',
                    true,
                    true
                ), function ($formatter) {
                    $formatter->includeStacktraces();
                }));
            }
        }
    }
    

    这将产生以下内容:

    [2020-05-13 16:52:24] MyPrefix.development.INFO: []  
    

    我们在这里做的是添加一个新的处理器并添加密钥prefix 到它。 然后我们可以在行格式器上使用这个键,并能够决定我们想用%prefix%在哪里显示它

    【讨论】:

    • 感谢@Remul - 正是我所追求的!
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    相关资源
    最近更新 更多