-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBuildLogger.php
More file actions
73 lines (57 loc) · 1.67 KB
/
Copy pathBuildLogger.php
File metadata and controls
73 lines (57 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace PHPCensor\Logging;
use PHPCensor\Model\Build;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class BuildLogger
{
private LoggerInterface $logger;
private Build $build;
public function __construct(LoggerInterface $logger, Build $build)
{
$this->logger = $logger;
$this->build = $build;
}
public function log($message, string $level = LogLevel::INFO, array $context = []): void
{
if (!\is_array($message)) {
$message = [$message];
}
$context['build'] = $this->build->getId();
foreach ($message as $item) {
$this->logger->log($level, $item, ($item ? $context : []));
}
}
public function logWarning(string $message): void
{
$this->log("\033[0;31m" . $message . "\033[0m");
}
public function logSuccess(string $message): void
{
$this->log("\033[0;32m" . $message . "\033[0m");
}
public function logFailure(string $message, ?\Throwable $exception = null): void
{
$level = LogLevel::INFO;
$context = [];
if ($exception) {
$level = LogLevel::ERROR;
$context['exception'] = $exception;
$context['trace'] = $exception->getTrace();
}
$this->log("\033[0;31m" . $message . "\033[0m", $level, $context);
}
public function logDebug(string $message): void
{
if ($this->build->isDebug()) {
$this->log("\033[0;36m" . $message . "\033[0m", LogLevel::DEBUG);
}
}
}