/
Logger.php
135 lines (109 loc) · 3.34 KB
/
Logger.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace SilverStripe\GraphQL\Schema;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injectable;
use Stringable;
class Logger implements LoggerInterface
{
use Injectable;
const BLACK = "\033[30m";
const RED = "\033[31m";
const GREEN = "\033[32m";
const YELLOW = "\033[33m";
const BLUE = "\033[34m";
const MAGENTA = "\033[35m";
const CYAN = "\033[36m";
const WHITE = "\033[37m";
const RESET = "\033[0m";
const DEBUG = 100;
const INFO = 200;
const NOTICE = 250;
const WARNING = 300;
const ERROR = 400;
const CRITICAL = 500;
const ALERT = 550;
const EMERGENCY = 600;
private int $level = self::INFO;
public function setVerbosity(int $level): self
{
$this->level = $level;
return $this;
}
public function alert(Stringable|string $message, array $context = []):void
{
if ($this->level > self::ALERT) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::RED);
}
public function critical(Stringable|string $message, array $context = []): void
{
if ($this->level > self::CRITICAL) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::RED);
}
public function debug(Stringable|string $message, array $context = []): void
{
if ($this->level > self::DEBUG) {
return;
}
$this->output($message, strtoupper(__FUNCTION__));
}
public function emergency(Stringable|string $message, array $context = []): void
{
if ($this->level > self::EMERGENCY) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::RED);
}
public function error(Stringable|string $message, array $context = []): void
{
if ($this->level > self::ERROR) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::RED);
}
public function info(Stringable|string $message, array $context = []): void
{
if ($this->level > self::INFO) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::CYAN);
}
public function log($level, Stringable|string $message, array $context = []): void
{
$this->output($message, strtoupper(__FUNCTION__));
}
public function notice(Stringable|string $message, array $context = []): void
{
if ($this->level > self::NOTICE) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::YELLOW);
}
public function warning(Stringable|string $message, array $context = []): void
{
if ($this->level > self::WARNING) {
return;
}
$this->output($message, strtoupper(__FUNCTION__), self::YELLOW);
}
public function output(string $msg, ?string $prefix = null, ?string $colour = null): void
{
$cli = Director::is_cli();
$formatted = sprintf(
'%s%s%s%s',
$colour && $cli ? $colour :'',
$prefix ? '[' . $prefix . ']: ' : '',
$colour && $cli ? self::RESET : '',
$msg
);
if ($cli) {
fwrite(STDOUT, $formatted . PHP_EOL);
} else {
echo $formatted . "<br>";
}
}
}