Skip to content

Commit 717fc00

Browse files
show log in Web UI see #224
1 parent 65d99c1 commit 717fc00

File tree

9 files changed

+169
-1
lines changed

9 files changed

+169
-1
lines changed

MANUAL.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ LDAP users cannot be created from the user management interface. User may be con
920920
But it's possible to automatically create new user as they connect for the first time using LDAP. To do this, __a default user class__ must be set in the admin panel.
921921

922922

923+
### Log file
924+
925+
W generate a log file called `w_error.log` located at the root of W installation path. It's also possible to browse a simplified version in the Web interface from the admin interface.
926+
923927

924928
Profile
925929
-------

app/class/Controlleradmin.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,27 @@ public function database(): void
9898
}
9999
$this->routedirect('admin');
100100
}
101+
102+
public function log(): void
103+
{
104+
if (!$this->user->isadmin()) {
105+
http_response_code(403);
106+
$this->showtemplate('forbidden');
107+
exit;
108+
}
109+
110+
$loglines = file(Model::ERROR_LOG);
111+
112+
$logs = [];
113+
foreach ($loglines as $line) {
114+
if (!str_starts_with($line, '#')) {
115+
try {
116+
$logs[] = new Logline($line);
117+
} catch (RuntimeException $e) {
118+
// Skip the line if parsing failed
119+
}
120+
}
121+
}
122+
$this->showtemplate('adminlog', ['logs' => $logs]);
123+
}
101124
}

app/class/Logline.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Wcms;
4+
5+
use DateTime;
6+
use DateTimeInterface;
7+
use RuntimeException;
8+
9+
class Logline
10+
{
11+
public DateTime $date;
12+
public string $level;
13+
public string $ref;
14+
public string $message;
15+
public string $in = '';
16+
17+
public const LEVELS = ['ERROR', 'WARN', 'INFO', 'DEBUG'];
18+
19+
/**
20+
* @throws RuntimeException if parsing failed
21+
*/
22+
public function __construct(string $line)
23+
{
24+
$length = strlen($line);
25+
$date = substr($line, 0, 25);
26+
$datetime = DateTime::createFromFormat(DateTimeInterface::ATOM, $date);
27+
28+
if ($datetime === false) {
29+
throw new RuntimeException("Date $date is not parsable from '$line'");
30+
}
31+
$this->date = $datetime;
32+
33+
$level = substr($line, 28, 5);
34+
$level = trim($level);
35+
if (in_array($level, self::LEVELS)) {
36+
$this->level = strtolower($level);
37+
}
38+
39+
$rest = substr($line, 36);
40+
41+
$refpos = strpos($rest, ' ');
42+
43+
if ($refpos === false) {
44+
throw new RuntimeException();
45+
}
46+
$this->ref = substr($rest, 0, $refpos);
47+
48+
$rest = substr($rest, $refpos + 1);
49+
50+
$inpos = strrpos($rest, ' in /');
51+
52+
if ($inpos === false) {
53+
$this->message = $rest;
54+
} else {
55+
$this->message = substr($rest, 0, $inpos);
56+
$this->in = substr($rest, $inpos + 4);
57+
}
58+
}
59+
}

app/class/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class Model
2727
public const FONTS_CSS_FILE = self::CSS_DIR . 'fonts.css';
2828
public const DATABASE_DIR = './database/';
2929
public const PAGES_DIR = self::DATABASE_DIR . 'pages/';
30+
public const ERROR_LOG = 'w_error.log';
3031

3132
public const FILE_PERMISSION = 0660;
3233
public const FOLDER_PERMISSION = 0770;

app/class/Routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function match(): void
5959
['POST', '/!media/edit', 'Controllermedia#edit', 'mediaedit'],
6060
['POST', '/!media/rename', 'Controllermedia#rename', 'mediarename'],
6161
['GET', '/!admin', 'Controlleradmin#desktop', 'admin'],
62+
['GET', '/!admin/log', 'Controlleradmin#log', 'adminlog'],
6263
['POST', '/!admin', 'Controlleradmin#update', 'adminupdate'],
6364
['POST', '/!admin/database', 'Controlleradmin#database', 'admindatabase'],
6465
['GET', '/!user', 'Controlleruser#desktop', 'user'],

app/view/templates/admin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<span>Save</span>
1010
</button>
1111
</form>
12+
<div class="hbar-section">
13+
<a href="<?= $this->url('adminlog') ?>">
14+
<i class="fa fa-align-left"></i>
15+
logs
16+
</a>
17+
</div>
1218
</nav>
1319

1420
<main class="admin grid">

app/view/templates/adminlog.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php $this->layout('layout', ['title' => 'admin', 'stylesheets' => [$css . 'back.css', $css . 'adminlog.css']]) ?>
2+
<?php $this->start('page') ?>
3+
<?php $this->insert('backtopbar', ['user' => $user, 'tab' => 'admin', 'pagelist' => $pagelist]) ?>
4+
5+
<main>
6+
<section>
7+
<h2>
8+
Logs
9+
</h2>
10+
<div class="scroll">
11+
<table>
12+
<thead>
13+
<tr>
14+
<th>
15+
date
16+
</th>
17+
<th>
18+
level
19+
</th>
20+
<th>
21+
message
22+
</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
<?php foreach($logs as $log) : ?>
27+
<tr>
28+
<td class="date">
29+
<?= $this->datemedium($log->date) ?>
30+
</td>
31+
<td class="level">
32+
<div class="<?= $log->level ?>">
33+
<?= $log->level ?>
34+
</div>
35+
</td>
36+
<td>
37+
<?= $log->message ?>
38+
</td>
39+
</tr>
40+
<?php endforeach ?>
41+
</tbody>
42+
</table>
43+
</div>
44+
</section>
45+
</main>
46+
47+
<?php $this->stop('page') ?>

assets/css/adminlog.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.level div {
2+
width: 70px;
3+
text-align: center;
4+
color: white;
5+
line-height: 20px;
6+
border-radius: var(--radius);
7+
}
8+
9+
.level div.warn {
10+
background-color: orange;
11+
}
12+
13+
.level div.error {
14+
background-color: red;
15+
16+
}
17+
18+
.level div.info {
19+
background-color: green;
20+
21+
}
22+
23+
.date {
24+
white-space: nowrap;
25+
}

index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
require('./vendor/autoload.php');
44
mb_internal_encoding('UTF-8');
55

6+
use Wcms\Model;
7+
68
try {
7-
Wcms\Logger::init('w_error.log', 3);
9+
Wcms\Logger::init(Model::ERROR_LOG, 3);
810
} catch (RuntimeException $e) {
911
die('Unable to init logs: ' . $e->getMessage());
1012
}

0 commit comments

Comments
 (0)