Deprecated log table #32
@ -11,8 +11,10 @@ class MySQL extends AbstractProcessingHandler
|
|||||||
{
|
{
|
||||||
private bool $initialized = false;
|
private bool $initialized = false;
|
||||||
private PDOStatement $statement;
|
private PDOStatement $statement;
|
||||||
|
private PDOStatement $statementDeprecated;
|
||||||
|
|
||||||
public function __construct(protected Connection $connection, protected int $retainDays = 90, int|string|Level $level = Level::Debug, bool $bubble = true)
|
public function __construct(protected Connection $connection, protected int $retainDays = 90,
|
||||||
|
int|string|Level $level = Level::Debug, bool $bubble = true)
|
||||||
{
|
{
|
||||||
parent::__construct($level, $bubble);
|
parent::__construct($level, $bubble);
|
||||||
}
|
}
|
||||||
@ -22,9 +24,24 @@ class MySQL extends AbstractProcessingHandler
|
|||||||
if (!$this->checkTableExists()) {
|
if (!$this->checkTableExists()) {
|
||||||
$this->createTable();
|
$this->createTable();
|
||||||
}
|
}
|
||||||
|
if (!$this->checkTableDeprecatedExists()) {
|
||||||
|
$this->createTableDeprecated();
|
||||||
|
}
|
||||||
$this->cleanup();
|
$this->cleanup();
|
||||||
|
$this->cleanupDeprecated();
|
||||||
$this->initialized();
|
$this->initialized();
|
||||||
}
|
}
|
||||||
|
if (str_contains(strtolower($record->message), 'deprecated:')) {
|
||||||
|
$this->statementDeprecated->execute([
|
||||||
|
'channel' => $record->channel,
|
||||||
|
'level' => $record->level->getName(),
|
||||||
|
'message' => $record->formatted,
|
||||||
|
'time' => $record->datetime->format('Y-m-d H:i:s.u'),
|
||||||
|
'context' => (count($record->context) > 0) ? json_encode($record->context, JSON_UNESCAPED_SLASHES) : '',
|
||||||
|
'extra' => (count($record->extra) > 0) ? json_encode($record->extra, JSON_UNESCAPED_SLASHES) : ''
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
$this->statement->execute([
|
$this->statement->execute([
|
||||||
'channel' => $record->channel,
|
'channel' => $record->channel,
|
||||||
'level' => $record->level->getName(),
|
'level' => $record->level->getName(),
|
||||||
@ -42,6 +59,11 @@ INSERT INTO monolog (channel, level, message, time, context, extra)
|
|||||||
VALUES (:channel, :level, :message, :time, :context, :extra)
|
VALUES (:channel, :level, :message, :time, :context, :extra)
|
||||||
QUERY;
|
QUERY;
|
||||||
$this->statement = $this->connection->getPDO()->prepare($query);
|
$this->statement = $this->connection->getPDO()->prepare($query);
|
||||||
|
$query = <<<QUERY
|
||||||
|
INSERT INTO monolog_deprecated (channel, level, message, time, context, extra)
|
||||||
|
VALUES (:channel, :level, :message, :time, :context, :extra)
|
||||||
|
QUERY;
|
||||||
|
$this->statementDeprecated = $this->connection->getPDO()->prepare($query);
|
||||||
$this->initialized = true;
|
$this->initialized = true;
|
||||||
}
|
}
|
||||||
private function checkTableExists(): bool
|
private function checkTableExists(): bool
|
||||||
@ -50,6 +72,12 @@ QUERY;
|
|||||||
$result = $this->connection->query($query);
|
$result = $this->connection->query($query);
|
||||||
return $result->rowCount() > 0;
|
return $result->rowCount() > 0;
|
||||||
}
|
}
|
||||||
|
private function checkTableDeprecatedExists(): bool
|
||||||
|
{
|
||||||
|
$query = "SHOW TABLES LIKE 'monolog_deprecated'";
|
||||||
|
$result = $this->connection->query($query);
|
||||||
|
return $result->rowCount() > 0;
|
||||||
|
}
|
||||||
private function createTable(): void
|
private function createTable(): void
|
||||||
{
|
{
|
||||||
$query = <<<QUERY
|
$query = <<<QUERY
|
||||||
@ -64,9 +92,27 @@ CREATE TABLE IF NOT EXISTS monolog (
|
|||||||
QUERY;
|
QUERY;
|
||||||
$this->connection->getPDO()->exec($query);
|
$this->connection->getPDO()->exec($query);
|
||||||
}
|
}
|
||||||
|
private function createTableDeprecated(): void
|
||||||
|
{
|
||||||
|
$query = <<<QUERY
|
||||||
|
CREATE TABLE IF NOT EXISTS monolog_deprecated (
|
||||||
|
channel VARCHAR(255),
|
||||||
|
level VARCHAR(100),
|
||||||
|
message LONGTEXT,
|
||||||
|
time DATETIME,
|
||||||
|
context LONGTEXT,
|
||||||
|
extra LONGTEXT
|
||||||
|
)
|
||||||
|
QUERY;
|
||||||
|
|
||||||
|
}
|
||||||
private function cleanup(): void
|
private function cleanup(): void
|
||||||
{
|
{
|
||||||
$query = "DELETE FROM monolog WHERE time < DATE_SUB(CURDATE(), INTERVAL {$this->retainDays} DAY)";
|
$query = "DELETE FROM monolog WHERE time < DATE_SUB(CURDATE(), INTERVAL {$this->retainDays} DAY)";
|
||||||
$this->connection->query($query);
|
$this->connection->query($query);
|
||||||
}
|
}
|
||||||
|
private function cleanupDeprecated(): void
|
||||||
|
{
|
||||||
|
$query = "DELETE FROM monolog_deprecated WHERE time < DATE_SUB(CURDATE(), INTERVAL {$this->retainDays} DAY)";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user