FIX: Cleanup after create, but before initialized query.

This commit is contained in:
Juan Pablo Vial
2025-06-02 18:03:43 -04:00
parent 7d8e2249de
commit c1149d89be

View File

@ -19,9 +19,12 @@ class MySQLHandler extends AbstractProcessingHandler
public function write(LogRecord $record): void public function write(LogRecord $record): void
{ {
if (!$this->initialized) { if (!$this->initialized) {
if (!$this->checkTableExists()) {
$this->createTable();
}
$this->cleanup();
$this->initialized(); $this->initialized();
} }
$this->cleanup();
$this->statement->execute([ $this->statement->execute([
'channel' => $record->channel, 'channel' => $record->channel,
'level' => $record->level->getName(), 'level' => $record->level->getName(),
@ -35,6 +38,21 @@ class MySQLHandler extends AbstractProcessingHandler
private function initialized(): void private function initialized(): void
{ {
$query = <<<QUERY $query = <<<QUERY
INSERT INTO monolog (channel, level, message, time, context, extra)
VALUES (:channel, :level, :message, :time, :context, :extra)
QUERY;
$this->statement = $this->connection->getPDO()->prepare($query);
$this->initialized = true;
}
private function checkTableExists(): bool
{
$query = "SHOW TABLES LIKE 'monolog'";
$result = $this->connection->query($query);
return $result->rowCount() > 0;
}
private function createTable(): void
{
$query = <<<QUERY
CREATE TABLE IF NOT EXISTS monolog ( CREATE TABLE IF NOT EXISTS monolog (
channel VARCHAR(255), channel VARCHAR(255),
level VARCHAR(100), level VARCHAR(100),
@ -45,12 +63,6 @@ CREATE TABLE IF NOT EXISTS monolog (
) )
QUERY; QUERY;
$this->connection->getPDO()->exec($query); $this->connection->getPDO()->exec($query);
$query = <<<QUERY
INSERT INTO monolog (channel, level, message, time, context, extra)
VALUES (:channel, :level, :message, :time, :context, :extra)
QUERY;
$this->statement = $this->connection->getPDO()->prepare($query);
$this->initialized = true;
} }
private function cleanup(): void private function cleanup(): void
{ {