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);
}
}
}