Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Log | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
sanitizeMessage | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
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; |
11 | |
12 | /** |
13 | * Class Log. |
14 | * |
15 | * @package log |
16 | */ |
17 | class Log implements \Stringable |
18 | { |
19 | public readonly LogLevel $level; |
20 | public readonly string $message; |
21 | public readonly int $time; |
22 | public readonly string $id; |
23 | |
24 | public function __construct(LogLevel $level, string $message, int $time, string $id) |
25 | { |
26 | $this->level = $level; |
27 | $this->message = $this->sanitizeMessage($message); |
28 | $this->time = $time; |
29 | $this->id = $id; |
30 | } |
31 | |
32 | public function __toString() : string |
33 | { |
34 | return \implode(' ', [ |
35 | \date('Y-m-d H:i:s', $this->time), |
36 | $this->level->name, |
37 | $this->id, |
38 | $this->message, |
39 | ]); |
40 | } |
41 | |
42 | protected function sanitizeMessage(string $message) : string |
43 | { |
44 | $message = \explode(\PHP_EOL, $message); |
45 | $lines = []; |
46 | foreach ($message as $line) { |
47 | $line = \trim($line); |
48 | if ($line !== '') { |
49 | $lines[] = $line; |
50 | } |
51 | } |
52 | return \implode(\PHP_EOL, $lines); |
53 | } |
54 | } |