Symfony/Monolog: 登录数据库 --> 序列化错误

Symfony/Monolog: Log into database --> Serialization Error

背景

我想将 symfony / monolog 日志保存到我的数据库中。我遵循了这个guide,它实际上给出了一个很好的解决方案。

问题

我的解决方案总体上是有效的,但在尝试保存 CONTEXT 变量时出现错误 (Serialization of 'Closure' is not allowed)。我不知道有什么问题或如何解决它。

有什么想法吗?非常感谢!

class WebsiteLogger {
    [...]

    /**
     * @ORM\Column(type="string", type="array", nullable=true)
     */
    private $context;

    /**
     * @ORM\Column(type="string", type="array", nullable=true)
     */
    private $extra;

    [...]
}
class DatabaseHandler extends AbstractProcessingHandler {
    private $emi;

    public function __construct(EntityManagerInterface $emi) {
        $this->emi        = $emi;
        parent::__construct();
    }

    protected function write(array $record): void {

        try {

            // store into database
            $logItem = new WebsiteLogger();
            $logItem->setChannel($record['channel']);
            $logItem->setLevel($record['level']);
            $logItem->setLevelName($record['level_name']);
            $logItem->setMessage($record['message']);
            $logItem->setLoggedAt($record['datetime']);
            $logItem->setExtra($record['extra']);
            $logItem->setContext(["initialization" => "not replaced yet"]);

            $this->emi->persist($logItem);
            $this->emi->flush();

        } catch ( Exception $exception ) {
            return;
        }


        try {
            $logItem->setContext($record['context']);

            $this->emi->persist($logItem); // this is causing the issue
            $this->emi->flush();

        } catch (Exception $exception) {
            dump("save CONTEXT - exception");
            dump($exception);
        }


    }
}

您可以取消设置数组中的所有闭包:

$context = $record['context'];
foreach ($context as $key => $value) {
    if ($value instanceof \Closure) {
        unset($context[$key]);
    }
}

但是不支持多维数组。您必须确定闭包的来源并防止它出现在上下文中。

我的解决方案

在 symfony 中有一个很好的 class 叫做 FlattenException。这有助于序列化异常并使其可存储到数据库中。

玩得开心!

    // ...

    protected function write(array $record): void {

        try {

            // store into database
            $logItem = new WebsiteLogger();
            $logItem->setChannel($record['channel']);
            $logItem->setLevel($record['level']);
            $logItem->setLevelName($record['level_name']);
            $logItem->setMessage($record['message']);
            $logItem->setLoggedAt($record['datetime']);
            $logItem->setExtra($record['extra']);

            // set a default value that shall be replaced but will be kept in case an exception will be thrown.
            $logItem->setContext(["initialization" => "not replaced (= exception thrown while trying to save the context)"]);

            $this->emi->persist($logItem);
            $this->emi->flush();

        } catch ( Exception $exception ) {
            return;
        }


        try {
           
            $exception = $record['context']['exception'] ?? null;

            if ($exception && $exception instanceof Throwable ) {
                $flattenException = FlattenException::createFromThrowable($exception);

                $context = $flattenException;
            } else {
                $context = $record['context'];
            }

            $logItem->setContext($context);

            $this->emi->persist($logItem);
            $this->emi->flush();

        } catch (Exception $exception) {
            // do nothing
        }


    }