Removes obsolete insertLog()
parent
bbccb54059
commit
81b4187ae8
|
@ -29,14 +29,12 @@ class Log {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate insertLog to underlying logger
|
* PSR-3 compatible log method
|
||||||
*
|
* @param string $level
|
||||||
* @param mixed $userId
|
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @param string|null $scope
|
* @param array $context
|
||||||
* @return mixed True on success or error message
|
|
||||||
*/
|
*/
|
||||||
public function insertLog($userId, string $message, ?string $scope = null) {
|
public function log(string $level, string $message, array $context = []): void {
|
||||||
return $this->logger->insertLog($userId, $message, $scope);
|
$this->logger->log($level, $message, $context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,17 +8,7 @@ namespace App\Core;
|
||||||
class NullLogger
|
class NullLogger
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* No-op insertLog.
|
* PSR-3 compatible log stub.
|
||||||
*
|
|
||||||
* @param mixed $userId
|
|
||||||
* @param string $message
|
|
||||||
* @param string|null $type
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function insertLog($userId, string $message, ?string $type = null): void {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PSR-3 log stub.
|
|
||||||
* @param string $level
|
* @param string $level
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @param array $context
|
* @param array $context
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* Returns a logger instance: plugin Log if available, otherwise NullLogger.
|
* Returns a logger instance: plugin Log if available, otherwise NullLogger.
|
||||||
*
|
*
|
||||||
* @param mixed $database Database or DatabaseConnector instance.
|
* @param mixed $database Database or DatabaseConnector instance.
|
||||||
* @return mixed Logger instance with insertLog() method.
|
* @return mixed Logger instance with PSR-3 log() compatible method.
|
||||||
*/
|
*/
|
||||||
function getLoggerInstance($database) {
|
function getLoggerInstance($database) {
|
||||||
if (class_exists('Log')) {
|
if (class_exists('Log')) {
|
||||||
|
|
|
@ -21,36 +21,6 @@ class Log {
|
||||||
$this->db = $database->getConnection();
|
$this->db = $database->getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert a log event into the database.
|
|
||||||
*
|
|
||||||
* @param int $userId The ID of the user associated with the log event.
|
|
||||||
* @param string $message The log message to insert.
|
|
||||||
* @param string $scope The scope of the log event (e.g., 'user', 'system'). Default is 'user'.
|
|
||||||
*
|
|
||||||
* @return bool|string True on success, or an error message on failure.
|
|
||||||
*/
|
|
||||||
public function insertLog($userId, $message, $scope = 'user') {
|
|
||||||
try {
|
|
||||||
$sql = 'INSERT INTO log
|
|
||||||
(user_id, scope, message)
|
|
||||||
VALUES
|
|
||||||
(:user_id, :scope, :message)';
|
|
||||||
|
|
||||||
$query = $this->db->prepare($sql);
|
|
||||||
$query->execute([
|
|
||||||
':user_id' => $userId,
|
|
||||||
':scope' => $scope,
|
|
||||||
':message' => $message,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve log entries from the database.
|
* Retrieve log entries from the database.
|
||||||
*
|
*
|
||||||
|
@ -67,8 +37,8 @@ class Log {
|
||||||
$where_clauses = [];
|
$where_clauses = [];
|
||||||
|
|
||||||
// Base query with user join
|
// Base query with user join
|
||||||
$base_sql = 'SELECT l.*, u.username
|
$base_sql = 'SELECT l.*, u.username
|
||||||
FROM log l
|
FROM log l
|
||||||
LEFT JOIN user u ON l.user_id = u.id';
|
LEFT JOIN user u ON l.user_id = u.id';
|
||||||
|
|
||||||
// Add scope condition
|
// Add scope condition
|
||||||
|
@ -120,10 +90,29 @@ class Log {
|
||||||
return $query->fetchAll(PDO::FETCH_ASSOC);
|
return $query->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PSR-3 style log method
|
/**
|
||||||
|
* PSR-3 style log method - inserts a log event into the database.
|
||||||
|
*
|
||||||
|
* @param string $level The log level (emergency, alert, critical, error, warning, notice, info, debug).
|
||||||
|
* @param string $message The log message to insert.
|
||||||
|
* @param string $scope The scope of the log event (e.g., 'user', 'system'). Default is 'system'.
|
||||||
|
*/
|
||||||
public function log(string $level, string $message, array $context = []): void {
|
public function log(string $level, string $message, array $context = []): void {
|
||||||
$userId = $context['user_id'] ?? null;
|
$userId = $context['user_id'] ?? null;
|
||||||
$scope = $context['scope'] ?? 'system';
|
$scope = $context['scope'] ?? 'system';
|
||||||
$this->insertLog($userId, "[$level] " . $message, $scope);
|
try {
|
||||||
|
$sql = 'INSERT INTO log
|
||||||
|
(user_id, scope, message)
|
||||||
|
VALUES
|
||||||
|
(:user_id, :scope, :message)';
|
||||||
|
$query = $this->db->prepare($sql);
|
||||||
|
$query->execute([
|
||||||
|
':user_id' => $userId,
|
||||||
|
':scope' => $scope,
|
||||||
|
':message' => "[$level] " . $message,
|
||||||
|
]);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// swallowing exceptions or here we could log to error log for testing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue