Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
65 / 65 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| LogCollector | |
100.00% |
65 / 65 |
|
100.00% |
4 / 4 |
13 | |
100.00% |
1 / 1 |
| setLogger | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getActivities | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| getContents | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
5 | |||
| renderLogs | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Log Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Log\Debug; |
| 11 | |
| 12 | use Framework\Debug\Collector; |
| 13 | use Framework\Log\Logger; |
| 14 | use Framework\Log\LogLevel; |
| 15 | |
| 16 | /** |
| 17 | * Class LogCollector. |
| 18 | * |
| 19 | * @package log |
| 20 | */ |
| 21 | class LogCollector extends Collector |
| 22 | { |
| 23 | protected Logger $logger; |
| 24 | |
| 25 | public function setLogger(Logger $logger) : static |
| 26 | { |
| 27 | $this->logger = $logger; |
| 28 | return $this; |
| 29 | } |
| 30 | |
| 31 | public function getActivities() : array |
| 32 | { |
| 33 | $activities = []; |
| 34 | foreach ($this->getData() as $index => $data) { |
| 35 | $activities[] = [ |
| 36 | 'collector' => $this->getName(), |
| 37 | 'class' => static::class, |
| 38 | 'description' => 'Set log ' . ($index + 1), |
| 39 | 'start' => $data['start'], |
| 40 | 'end' => $data['end'], |
| 41 | ]; |
| 42 | } |
| 43 | return $activities; |
| 44 | } |
| 45 | |
| 46 | public function getContents() : string |
| 47 | { |
| 48 | if ( ! isset($this->logger)) { |
| 49 | return '<p>A Logger instance has not been set on this collector.</p>'; |
| 50 | } |
| 51 | \ob_start(); ?> |
| 52 | <p><strong>Logger:</strong> <?= $this->logger::class ?></p> |
| 53 | <?php |
| 54 | $destination = $this->logger->getDestination(); |
| 55 | if ($destination): |
| 56 | ?> |
| 57 | <p><strong>Destination:</strong> <?= \htmlentities($destination) ?></p> |
| 58 | <?php |
| 59 | endif; |
| 60 | $level = $this->logger->getLevel(); ?> |
| 61 | <p><strong>Log Level:</strong> <?= \htmlentities( |
| 62 | $level->value . ' ' . $level->name |
| 63 | ) ?> |
| 64 | </p> |
| 65 | <h1>Logs</h1> |
| 66 | <?= $this->renderLogs() ?> |
| 67 | <h1>Available Levels</h1> |
| 68 | <table> |
| 69 | <thead> |
| 70 | <tr> |
| 71 | <th>Level</th> |
| 72 | <th>Name</th> |
| 73 | </tr> |
| 74 | </thead> |
| 75 | <tbody> |
| 76 | <?php foreach (LogLevel::cases() as $case): ?> |
| 77 | <tr<?= $case === $this->logger->getLevel() |
| 78 | ? ' class="active" title="Current level"' |
| 79 | : '' ?>> |
| 80 | <td><?= $case->value ?></td> |
| 81 | <td><?= \htmlentities($case->name) ?></td> |
| 82 | </tr> |
| 83 | <?php endforeach ?> |
| 84 | </tbody> |
| 85 | </table> |
| 86 | <?php |
| 87 | return \ob_get_clean(); // @phpstan-ignore-line |
| 88 | } |
| 89 | |
| 90 | protected function renderLogs() : string |
| 91 | { |
| 92 | if ( ! $this->hasData()) { |
| 93 | return '<p>No log has been set.</p>'; |
| 94 | } |
| 95 | $count = \count($this->getData()); |
| 96 | \ob_start(); ?> |
| 97 | <p><?= $count ?> log<?= $count === 1 ? ' has' : 's have' ?> been set.</p> |
| 98 | <table> |
| 99 | <thead> |
| 100 | <tr> |
| 101 | <th>#</th> |
| 102 | <th>Date</th> |
| 103 | <th>Time</th> |
| 104 | <th>Id</th> |
| 105 | <th colspan="2">Level</th> |
| 106 | <th>Message</th> |
| 107 | <th>Written</th> |
| 108 | <th title="Seconds">Time to Log</th> |
| 109 | </tr> |
| 110 | </thead> |
| 111 | <tbody> |
| 112 | <?php foreach ($this->getData() as $index => $data): ?> |
| 113 | <tr> |
| 114 | <td><?= $index + 1 ?></td> |
| 115 | <td><?= \htmlentities($data['date']) ?></td> |
| 116 | <td><?= \htmlentities($data['time']) ?></td> |
| 117 | <td><?= \htmlentities($data['id']) ?></td> |
| 118 | <td><?= \htmlentities((string) $data['level']) ?></td> |
| 119 | <td><?= \htmlentities($data['levelName']) ?></td> |
| 120 | <td> |
| 121 | <pre><code class="language-log"><?= \htmlentities($data['message']) ?></code></pre> |
| 122 | </td> |
| 123 | <td><?= $data['written'] ? 'Yes' : 'No' ?></td> |
| 124 | <td><?= \round($data['end'] - $data['start'], 6) ?></td> |
| 125 | </tr> |
| 126 | <?php endforeach ?> |
| 127 | </tbody> |
| 128 | </table> |
| 129 | <?php |
| 130 | return \ob_get_clean(); // @phpstan-ignore-line |
| 131 | } |
| 132 | } |