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
{
if (!$this->initialized) {
$this->initialized();
if (!$this->checkTableExists()) {
$this->createTable();
}
$this->cleanup();
$this->initialized();
}
$this->statement->execute([
'channel' => $record->channel,
'level' => $record->level->getName(),
@ -35,6 +38,21 @@ class MySQLHandler extends AbstractProcessingHandler
private function initialized(): void
{
$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 (
channel VARCHAR(255),
level VARCHAR(100),
@ -45,12 +63,6 @@ CREATE TABLE IF NOT EXISTS monolog (
)
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
{