Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Background

I want to save symfony / monolog Logs into my database. I followed this guide which is acutally giving a good solution.

Problem

My solution is working in general, but I get errors (Serialization of 'Closure' is not allowed) while trying to save the CONTEXT variable. I have no idea, what is the problem or how to fix it.

Any idea? Many Thanks!

class WebsiteLogger {
    [...]

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

    /**
     * @ORMColumn(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);
        }


    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
313 views
Welcome To Ask or Share your Answers For Others

1 Answer

My Solution

There is a nice class in symfony called FlattenException. This is helping to serialize the exception and make it storable into the database.

Have fun!

    // ...

    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
        }


    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...