Adds proper log levels to log plugin

main
Yasen Pramatarov 2025-04-27 21:20:14 +03:00
parent 81b4187ae8
commit d886bcf755
5 changed files with 14 additions and 25 deletions

View File

@ -174,22 +174,6 @@ INSERT INTO `security_ip_whitelist` (`id`, `ip_address`, `is_network`, `descript
(4, '172.16.0.0/12', 1, 'Private network (Class B)', '2025-01-03 16:40:15', 'system'), (4, '172.16.0.0/12', 1, 'Private network (Class B)', '2025-01-03 16:40:15', 'system'),
(5, '192.168.0.0/16', 1, 'Private network (Class C)', '2025-01-03 16:40:15', 'system'); (5, '192.168.0.0/16', 1, 'Private network (Class C)', '2025-01-03 16:40:15', 'system');
--
-- Logs
--
-- --------------------------------------------------------
CREATE TABLE `log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`time` datetime NOT NULL DEFAULT current_timestamp(),
`scope` set('user','system') NOT NULL,
`message` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `log_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci;
-- --
-- Jilo -- Jilo
-- --

View File

@ -91,6 +91,7 @@ if (!empty($search)) {
$log_record = array( $log_record = array(
// assign title to the field in the array record // assign title to the field in the array record
'time' => $item['time'], 'time' => $item['time'],
'log level' => $item['level'],
'log message' => $item['message'] 'log message' => $item['message']
); );
} else { } else {
@ -99,6 +100,7 @@ if (!empty($search)) {
'userID' => $item['user_id'], 'userID' => $item['user_id'],
'username' => $item['username'], 'username' => $item['username'],
'time' => $item['time'], 'time' => $item['time'],
'log level' => $item['level'],
'log message' => $item['message'] 'log message' => $item['message']
); );
} }

View File

@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS `log` (
`id` INT(11) NOT NULL AUTO_INCREMENT, `id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL, `user_id` INT(11) NOT NULL,
`time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`level` VARCHAR(10) NOT NULL DEFAULT 'info', `level` set('emergency','alert','critical','error','warning','notice','info','debug') NOT NULL DEFAULT 'info',
`scope` set('user','system') NOT NULL, `scope` set('user','system') NOT NULL,
`message` VARCHAR(255) NOT NULL, `message` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),

View File

@ -102,14 +102,15 @@ class Log {
$scope = $context['scope'] ?? 'system'; $scope = $context['scope'] ?? 'system';
try { try {
$sql = 'INSERT INTO log $sql = 'INSERT INTO log
(user_id, scope, message) (user_id, level, scope, message)
VALUES VALUES
(:user_id, :scope, :message)'; (:user_id, :level, :scope, :message)';
$query = $this->db->prepare($sql); $query = $this->db->prepare($sql);
$query->execute([ $query->execute([
':user_id' => $userId, ':user_id' => $userId,
':level' => $level,
':scope' => $scope, ':scope' => $scope,
':message' => "[$level] " . $message, ':message' => $message,
]); ]);
} catch (Exception $e) { } catch (Exception $e) {
// swallowing exceptions or here we could log to error log for testing // swallowing exceptions or here we could log to error log for testing

View File

@ -75,13 +75,14 @@
<div class="mb-5"> <div class="mb-5">
<?php if (!empty($logs['records'])) { ?> <?php if (!empty($logs['records'])) { ?>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-hover align-middle mb-0"> <table class="table table-hover align-middle mb-0" style="width: 100%;">
<thead class="table-light"> <thead class="table-light">
<tr> <tr>
<?php if ($scope === 'system') { ?> <?php if ($scope === 'system') { ?>
<th>Username&nbsp;(id)</th> <th>Username&nbsp;(id)</th>
<?php } ?> <?php } ?>
<th>Time</th> <th style="white-space: nowrap;">Time</th>
<th style="white-space: nowrap;">Log level</th>
<th>Log message</th> <th>Log message</th>
</tr> </tr>
</thead> </thead>
@ -89,10 +90,11 @@
<?php foreach ($logs['records'] as $row) { ?> <?php foreach ($logs['records'] as $row) { ?>
<tr> <tr>
<?php if ($scope === 'system') { ?> <?php if ($scope === 'system') { ?>
<td><?= $row['userID'] ? '<strong>' . htmlspecialchars($row['username'] . " ({$row['userID']})") . '</strong>' : '<span class="text-muted font-weight-normal small">SYSTEM</span>' ?></td> <td style="white-space: nowrap;"><?= $row['userID'] ? '<strong>' . htmlspecialchars($row['username'] . " ({$row['userID']})") . '</strong>' : '<span class="text-muted font-weight-normal small">SYSTEM</span>' ?></td>
<?php } ?> <?php } ?>
<td><span class="text-muted"><?= date('d M Y H:i', strtotime($row['time'])) ?></span></td> <td style="white-space: nowrap;"><span class="text-muted"><?= date('d M Y H:i', strtotime($row['time'])) ?></span></td>
<td><?= htmlspecialchars($row['log message']) ?></td> <td style="white-space: nowrap;"><?= htmlspecialchars($row['log level']) ?></td>
<td style="width: 100%; word-break: break-word;"><?= htmlspecialchars($row['log message']) ?></td>
</tr> </tr>
<?php } ?> <?php } ?>
</tbody> </tbody>