forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildLogger.php
More file actions
114 lines (99 loc) · 2.65 KB
/
Copy pathBuildLogger.php
File metadata and controls
114 lines (99 loc) · 2.65 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace PHPCensor\Logging;
use PHPCensor\Model\Build;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Class BuildLogger
*/
class BuildLogger implements LoggerAwareInterface
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var Build
*/
protected $build;
/**
* Set up the BuildLogger class.
* @param LoggerInterface $logger
* @param Build $build
*/
public function __construct(LoggerInterface $logger, Build $build)
{
$this->logger = $logger;
$this->build = $build;
}
/**
* Add an entry to the build log.
* @param string|string[] $message
* @param string $level
* @param mixed[] $context
*/
public function log($message, $level = LogLevel::INFO, $context = [])
{
// Skip if no logger has been loaded.
if (!$this->logger) {
return;
}
if (!is_array($message)) {
$message = [$message];
}
// The build is added to the context so the logger can use
// details from it if required.
$context['build'] = $this->build;
foreach ($message as $item) {
$this->logger->log($level, $item, $context);
}
}
/**
* Add a success-coloured message to the log.
* @param string
*/
public function logSuccess($message)
{
$this->log("\033[0;32m" . $message . "\033[0m");
}
/**
* Add a failure-coloured message to the log.
* @param string $message
* @param \Exception $exception The exception that caused the error.
*/
public function logFailure($message, \Exception $exception = null)
{
$context = [];
// The psr3 log interface stipulates that exceptions should be passed
// as the exception key in the context array.
if ($exception) {
$context['exception'] = $exception;
$context['trace'] = $exception->getTrace();
}
$this->log("\033[0;31m" . $message . "\033[0m", LogLevel::ERROR, $context);
}
/**
* Add a debug message to the log.
* @param string
*/
public function logDebug($message)
{
if (
(defined('DEBUG_MODE') && DEBUG_MODE) ||
((boolean)$this->build->getExtra('debug'))
) {
$this->log("\033[0;36m" . $message . "\033[0m");
}
}
/**
* Sets a logger instance on the object
*
* @param LoggerInterface $logger
* @return null
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}