gpt4 book ai didi

php - Laravel Tap 格式化程序 : where do I actually format the message?

转载 作者:行者123 更新时间:2023-12-04 12:38:30 26 4
gpt4 key购买 nike

在我的 Laravel 应用程序中,需要格式化我需要发送到 slack 的消息。因此,我将一个松弛日志 channel 设置为 config/logging.php :

'slack'         => [
'driver' => 'slack',
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],

也如 documentation 上所见我可以做一个 monolog 格式化程序,因此我做了以下事情:
namespace App\Logging;

class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->setFormatter(...);
}
}
}

并将其指定为进入我的日志:
'slack'         => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],

但是在我的格式化程序中,我在哪里处理日志条目本身?我的意思是:
  • $handler->setFormatter好像不是\Illuminate\Log\Logger的方法类(class)。
  • 当我需要提供自定义格式时,我无法找到需要覆盖的方法。我的意思是我有 invoke 方法然后呢?
  • 最佳答案

    Dimitrios 几乎是正确的(或者这可能适用于旧版本)但正如 Xavier 所说,如果您完全复制该代码,您将收到错误

    Return value of Monolog\Handler\AbstractProcessingHandler::processRecord() must be of the type array, null returned
    你会想做这样的事情:
    <?php

    namespace App\Logging;

    use Monolog\Formatter\LineFormatter;
    use Monolog\Handler\SlackWebhookHandler;
    use Request;

    class CustomiseFormatter
    {
    protected $request;

    public function __construct(Request $request = null)
    {
    $this->request = $request;
    }

    public function __invoke($logger)
    {
    foreach ($logger->getHandlers() as $handler) {
    if ($handler instanceof SlackWebhookHandler) {
    $handler->setFormatter(new LineFormatter(
    '[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
    ));

    $handler->pushProcessor([$this, 'processLogRecord']);
    }
    }
    }

    public function processLogRecord(array $record): array
    {
    $record['extra'] += [
    'url' => env("APP_URL"),
    ];

    return $record;
    }
    }

    同样,如何 extra已修改,您可以更改任何其他值。

    关于php - Laravel Tap 格式化程序 : where do I actually format the message?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641137/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com